简体   繁体   English

我如何浏览json?

[英]How can i navigate through the json?

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. 我在对象中有一些JSON,但似乎可以返回JSON示例的值,如下所示。

{
"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 我正在尝试使用以下方法返回rootLayout

 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. 我是json的新手,我认为我被深深吸引了。 I cannot find any good reading on the internet can anyone recommend some. 我在互联网上找不到任何好的读物,任何人都可以推荐一些。

Thanks. 谢谢。

Some explanation because you don't seem to understand JSON 一些解释,因为您似乎不了解JSON

It's not as complicated as one may think. 它并不像人们想象的那么复杂。 It actually represents javascript objects as if they'd be written by code. 它实际上代表了javascript对象,就好像它们是由代码编写的一样。

So if you have JSON written as: 因此,如果您将JSON编写为:

{
    id : 100,
    name: "Yeah baby"
}

This means that your object has two properties: id and name . 这意味着您的对象具有两个属性: idname 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 . 在您的示例中,您可以看到您的对象具有两个属性: rootLayoutlayoutDescriptions The first one jsonObj.rootLayout is string and will return "main" and the second one is an array: 第一个jsonObj.rootLayout是字符串,将返回"main" ,第二个是数组:

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... 您在示例中提供的特定数组元素对象具有其自身的属性,就像我为顶级对象所解释的那样: id (字符串), container (另一个对象,因为它再次被包裹在curlies中)等。

I hope you understand JSON notation a bit more. 希望您对JSON表示法有所了解。

So let's go to your question then 那我们去问你的问题

You can get to id by accessing it via: 您可以通过以下方式访问id

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. 请注意,这只会枚举第一个内容对象的内容对象...如果这是有道理的...那么看一下JSON对象,您将看到嵌套了对象的内容数组。

The object is an object, not an array, and it doesn't have a property called 0 . 对象是对象,而不是数组,并且没有名为0的属性。

To get rootLayout : 要获得rootLayout

obj.rootLayout

However, rootLayout is a string, not an object. 但是, rootLayout是字符串,而不是对象。 It doesn't have an id . 没有id The first item in the layoutDescriptions array does. layoutDescriptions数组中的第一项是。

obj.layoutDescriptions[0].id

Are you trying to get one of layoutDescriptions with id equals to obj.rootLayout ? 你们是不是要得到一个layoutDescriptionsid等于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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM