I have some JSON which I have in a object but I can seem to return the values a sample of the json is as follows.
{
"rootLayout":"main",
"layoutDescriptions":[
{
"id":"main",
"container" : {
"type":"Tabs",
"content":[
{
"type":"Panel",
"label":"Simple Address",
"layout":"SimpleForm",
"comment":"This form is simple name value pairs",
"content":[
{ "type":"label", "constraint":"newline", "text":"Org Name" },
{ "type":"text", "property":"propOne" },
{ "type":"label", "constraint":"newline", "text":"Address" },
{ "type":"text", "property":"addrLine1" },
{ "type":"text", "property":"addrLine2" },
{ "type":"text", "property":"addrLine3" },
{ "type":"label", "constraint":"newline", "text":"Postcode" },
{ "type":"text", "property":"postcode" }
]
},
I am trying to return the rootLayout using
obj[0].rootLayout.id
This doesn’t work also I am wondering how to access the content elements.
I am new to json and I have been thrown in the deep end I think. I cannot find any good reading on the internet can anyone recommend some.
Thanks.
Some explanation because you don’t seem to understand JSON
It’s not as complicated as one may think. It actually represents javascript objects as if they’d be written by code.
So if you have JSON written as:
This means that your object has two properties:
idandname. The first one is numeric and the second one is string.In your example you can see that your object has two properties:
rootLayoutandlayoutDescriptions. The first onejsonObj.rootLayoutis string and will return"main"and the second one is an array:Apparently an array of objects because array elements are enclosed in curly braces. This particular array element object that you provided in your example has its own properties just like I’ve explained for the top level object:
id(string),container(another object because it’s again enclosed in curlies) etc…I hope you understand JSON notation a bit more.
So let’s go to your question then
You can get to
idby accessing it via:and further getting to your content objects:
Mind that this will only enumerate first content object’s content objects… If this makes sense… Well look at your JSON object and you’ll see that you have nested content array of objects.