Good day everyone. I am trying to simulate (with php), an xml-like structure for json
as follows:
{
"Articles": [
{
"Article": {
"ID": 111,
"title": Origin of man",
"author": "Man and Woman",
"pubDate": "21st Feb, 2014",
"summary": "Both Idiots"
}
},
{
"Article": {
"ID": 222,
"title": "Origin of Animals",
"author": "Animal and Man",
"pubDate": "22nd Feb, 2014",
"summary": "Yet another Idiots"
}
},
{
"Article": {
"ID": 333,
"title": "Origin of Politicians",
"author": "Politicians and Man",
"pubDate": "23rd Feb, 2014",
"summary": "Inexplicable Idiots"
}
}
]
}
Here, "Articles" is the root node equvalence of json (as in xml)
and "Article" is the child nodes, with different IDs.
my question is, how can i programatically add a sub-child to
(or sub-record) to Article identified by 222, such that the
entires structure becomes something like this:
{
"Articles": [
{
"Article": {
"ID": 111,
"title": Origin of man",
"author": "Man and Woman",
"pubDate": "21st Feb, 2014",
"summary": "Both Idiot"
}
},
{
"Article": {
"ID": 222,
"title": "Origin of Animals":
"SUB-TITLES":
{
"SUB_TITLE_1": "Just kidding",
"SUB_TITLE_2": "Still Kiddng",
"SUB_TITLE_3": "Dont be offended"
}
"author": "Animal and Man",
"pubDate": "22nd Feb, 2014",
"summary": "Yet another Idiots"
}
},
{
"Article": {
"ID": 333,
"title": "Origin of Politicians",
"author": "Politicians and Man",
"pubDate": "23rd Feb, 2014",
"summary": "Inexplicable Idiots"
}
}
]
}
Here's my attempt, but doesnt seem good enough
though everything worked smoothly witout error,
but the output is not what i am actually expecting. Here's it:
{
"Articles": [
{
"Article": {
"ID":111,
"title":"Origin of man",
"author":"Man and Woman",
"pubDate":"21st Feb, 2014",
"summary":"Both Idiot"
}
},
{
"Article": {
"ID":222,
"title":"Origin of Animals":
//i expected to have SUB-TITLES record here, but didnt
"author":"Animal and Man",
"pubDate":"22nd Feb, 2014",
"summary":"Yet another Idiots"
}
},
{
"Article": {
"ID":333,
"title": "Origin of Politicians",
"author": "Politicians and Man",
"pubDate": "23rd Feb, 2014",
"summary": "Inexplicable Idiots"
}
}
],
//Now, here's where SUB-TITLES record showed up
"0":{ "SUB-TITLES": [
{"SUB_TITLE_1": "Just kidding"},
{"SUB_TITLE_2": "Still Kiddng"},
{"SUB_TITLE_3": "Dont be offended"}
]
}
}
As it is, i cant tell what exactly am doing wrong and i
cant also tell the source of that zero preceeding
the opening brace before "SUB-TITLES":, neither can i tell
from were the brace is coming. any guidiance or a code snipette
on how to do it well will be highly appreciated. thanks
to my helper in anticipation.
diafol
You don't show your code for producing the json. I'm assuming that you have a php array and you're using json_encode.
So, without your php code, difficult to know what to suggest.
You have errors in your json -= missing " and : instead of , (lines 48,57)
Edited by diafol
Osagie_1 0 Newbie Poster
Here's an extract of the code:
function JSONAddRecordToChild(array $Record, $ChildID)
{
$file = file_get_contents("File.json", true);
$data = json_decode($file, true); //get json by assoc array
unset($file); //prevent memory leaks for large json
//only when ID does not exist, write record to json
$node = $this->searchJSON($data["Articles"], $ChildID); //costumized searchJSON methode that returns a matching node
if($node != null){
$node = array("SUB-TITLES" => array());
foreach ($Record as $key => $value){
$node["SUB-TITLES"][] = array($key => $value);
}
foreach ($data as $child){
foreach ($child as $son){
if($son["Article"]['ID'] == $ChildID){
$data[] = $node;
}
}
}
echo json_encode($data).;
//to be written permanently to file
}
}
Sure ! i anticipate writing it to my file for permanent
storage if all goes fine. for now, i'm just displaying it on my browser
using:
echo json_encode($data);
once again, thanks in advance
Edited by Osagie_1
diafol
Was bored so cooked up a quick n dirty class. Seems to work Ok..
class jBuilder
{
public $phpArray=array();
public $containerName;
public function __construct($containerName=NULL)
{
if($containerName) $this->makeContainer($containerName);
}
public function makeContainer($containerName)
{
$this->containerName = $containerName;
}
private function makeJSON($data)
{
return json_encode($data, JSON_PRETTY_PRINT);
}
private function makeArrayItem($data)
{
return json_decode($data,true);
}
public function getJSON()
{
return $this->makeJSON(array($this->containerName=>$this->phpArray));
}
public function addJSONItem($JSONstring)
{
$item = $this->makeArrayItem($JSONstring);
$this->phpArray[] = $item;
}
public function addItem($itemKey, $itemArray)
{
$this->phpArray[] = array($itemKey=>$itemArray);
}
public function addArray($itemKey, $arrayOfItems)
{
foreach($arrayOfItems as $itemArray)
{
$this->addItem($itemKey, $itemArray);
}
}
public function sortOnField($itemKey, $sortField, $sort=SORT_ASC)
{
$out = array();
foreach ($this->phpArray as $key => $row)
{
$out[$key] = $row[$itemKey][$sortField];
}
array_multisort($out, $sort, $this->phpArray);
}
}
$c = new jBuilder("Articles");
$c->addItem("Article", array("ID"=>1,
"title"=>"Origin of man",
"author"=>"Man and Woman",
"pubDate"=>"21st Feb, 2014",
"summary"=>"Both Idiot"));
$c->addItem("Article", array("ID"=>100,
"title"=>"Origin of Politicians",
"author"=>"Politicians and Man",
"pubDate"=>"23rd Feb, 2014",
"summary"=>"Inexplicable Idiots"));
$c->addArray("Article", array(array("ID"=>22,
"title"=>"Bumbling Politicians",
"author"=>"Politicians and Man",
"pubDate"=>"23rd Feb, 2014",
"summary"=>"Inexplicable Idiots"),
array("ID"=>50,
"title"=>"Hole of man",
"author"=>"Man and Woman",
"pubDate"=>"21st Feb, 2014",
"summary"=>"Both Idiot")));
$JSONstring = '{
"Article": {
"ID":111,
"title":"ABel",
"author":"Man and Woman",
"pubDate":"21st Feb, 2014",
"summary":"Both Idiot"
}
}';
$c->addJSONItem($JSONstring);
$c->sortOnField("Article", "title");
echo "<pre>";
echo $c->getJSON();
cereal commented: good! +13
Osagie_1 0 Newbie Poster
A million thanks diafol. i seem to be getting it
rightly. but not with the removal of the trailing
array indices. the 0, 1, 2... ith are still showing
in the browser output. as in:
"0": { "SUB-TITLES": [
{"SUB_TITLE_1": "Just kidding"},
{"SUB_TITLE_2": "Still Kiddng"},
{"SUB_TITLE_3": "Dont be offended"}
]
}
"1": {}
"nth": {}..
is there anything that can be done to get rid of this
for ease of readability in future
once again, thanks !
diafol
Sorry missed thw bit on subtitles. On my pgone so forgive monkey fingers. Will try to post shortly
diafol
Sorry been away for weekend. Been thinking about the structure. Seems all wrong to me now.
If you have an object called articles, wouldn't it be easier to just have the objects without the key "article" - or maybe have the key as the id value
{"Articles":[{111:{"field1": value1,"field2": value2}},{50:{"field1": valueA,"field2": valueB}}]}
or
{"Articles":[{"ID": 111, "field1": value1,"field2": value2},{"ID": 50, "field1": valueA,"field2": valueB}]}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.