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.
Osagie_1 0
Newbie Poster
Recommended Answers
Jump to PostYou 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)
Jump to PostWas 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) …
All 6 Replies
diafol
Osagie_1 0
Newbie Poster
diafol
cereal
commented:
good! +13
Osagie_1 0
Newbie Poster
diafol
diafol
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.