最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i navigate through the json? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Aug 8, 2012 at 9:19 SagarmichaelSagarmichael 1,6247 gold badges24 silver badges54 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

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:

{
    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.

So let's go to your question then

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.

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

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);
发布评论

评论列表(0)

  1. 暂无评论