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
Are you trying to get one of layoutDescriptions
with id
equals to obj.rootLayout
?
var targetLayout = {};
for(var i = 0; i < obj.layoutDescriptions.length; i++) {
if(obj.layoutDescriptions[i].id == obj.rootLayout) {
targetLayout = obj.layoutDescriptions[i]; break;
}
}
console.log(targetLayout);
The object is an object, not an array, and it doesn't have a property called 0
.
To get rootLayout
:
obj.rootLayout
However, rootLayout
is a string, not an object. It doesn't have an id
. The first item in the layoutDescriptions
array does.
obj.layoutDescriptions[0].id
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:
{
id : 100,
name: "Yeah baby"
}
This means that your object has two properties: id
and name
. The first one is numeric and the second one is string.
In your example you can see that your object has two properties: rootLayout
and layoutDescriptions
. The first one jsonObj.rootLayout
is string and will return "main"
and the second one is an array:
layoutDescriptions: [ {...}, {...},... ]
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.
You can get to id
by accessing it via:
jsonObj.layoutDescriptions[0].id
and further getting to your content objects:
var contentObjects = jsonObj.layoutDescriptions[0].container.content[0].content;
for(var i = 0; i < contentObjects.length, i++)
{
// assign this inner object to a variable for simpler property access
var contObj = contentObjects[i];
// do with this object whatever you need to and access properties as
// contObj.type
// contObj.property
// contObj.text
// contObj.constraint
}
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.