对象和数组的复杂的嵌套JSON [英] Complex JSON nesting of objects and arrays

查看:236
本文介绍了对象和数组的复杂的嵌套JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与JSON对象/数组的语法和结构难以拥有的。

  {会计:[
                                {名字:约翰,
                                  姓氏:李四,
                                  时代:23},                                {名字:玛丽,
                                  姓氏:史密斯
                                  时代:32}
                              ]
              销售:[
                                {名字:莎莉,
                                  姓氏:绿,
                                  时代:27},                                {名字:吉姆,
                                  姓氏:厨房
                                  时代:41}
                              ]
            }

我要让对象和数组将容纳以下信息的嵌套结构:

  {
问题:[{
    糖尿病:[{
        药物:[{
            medicationsClasses:[{
                班级名称:[{
                    associatedDrug:[{
                        名:阿司匹林,
                        剂量:,
                        实力:500毫克
                    }],
                    associatedDrug#2:[{
                        名:somethingElse
                        剂量:,
                        实力:500毫克
                    }]
                }],
                className2:[{
                    associatedDrug:[{
                        名:阿司匹林,
                        剂量:,
                        实力:500毫克
                    }],
                    associatedDrug#2:[{
                        名:somethingElse
                        剂量:,
                        实力:500毫克
                    }]
                }]
            }]
        }],
        实验室:[{
            MISSING_FIELD:missing_value
        }]
    }],
    哮喘:[{}]
}]

}

但我不知道什么是正确的方式做,这应该是。如果我只是做将JavaScript对象? JSON是否有意义这个项目?

什么是正确的语法来设置这样的事情呢?

下面是我的code迄今:

  $(文件)。就绪(函数(){
    $ .getJSON(JS / orders.json',函数(JSON){
      $。每个(json.problems,功能(索引,排序){
        $('。loadMeds')追加('&所述p为H.;'+ order.name +'&下; / P>')
      });
    });
});


解决方案

第一个code是的Javascript code,这是类似,但不是JSON的例子。 JSON也不会有1)评论和2) VAR 关键字

您不必在你的JSON任何评论,但你应该删除 VAR ,并开始是这样的:

 命令:{

[{}] 符号表示在一个数组对象,而不是你所需要的任何地方。这不是错误,但它是为了某些目的太复杂了。 AssociatedDrug应该工作以及对象:

 associatedDrug:{
                名:阿司匹林,
                剂量:,
                实力:500毫克
          }

此外,空对象实验室应充满东西。

除此之外,您的code是好的。你可以将其粘贴到JavaScript或使用 JSON.parse()来方法,或任何其他解析方法(请的不使用eval

更新2回答道:

<$p$p><$c$c>obj.problems[0].Diabetes[0].medications[0].medicationsClasses[0].className[0].associatedDrug[0].name

返回'阿司匹林'。这不过是更适合于foreaches到处

I am having difficultly with syntax and structure of json objects/arrays.

 { "accounting" : [   
                                { "firstName" : "John",  
                                  "lastName"  : "Doe",
                                  "age"       : 23 },

                                { "firstName" : "Mary",  
                                  "lastName"  : "Smith",
                                  "age"       : 32 }
                              ],                            
              "sales"       : [ 
                                { "firstName" : "Sally", 
                                  "lastName"  : "Green",
                                  "age"       : 27 },

                                { "firstName" : "Jim",   
                                  "lastName"  : "Galley",
                                  "age"       : 41 }
                              ] 
            } 

I want to make a nested structure of objects and arrays that would house the following info:

{
"problems": [{
    "Diabetes":[{
        "medications":[{
            "medicationsClasses":[{
                "className":[{
                    "associatedDrug":[{
                        "name":"asprin",
                        "dose":"",
                        "strength":"500 mg"
                    }],
                    "associatedDrug#2":[{
                        "name":"somethingElse",
                        "dose":"",
                        "strength":"500 mg"
                    }]
                }],
                "className2":[{
                    "associatedDrug":[{
                        "name":"asprin",
                        "dose":"",
                        "strength":"500 mg"
                    }],
                    "associatedDrug#2":[{
                        "name":"somethingElse",
                        "dose":"",
                        "strength":"500 mg"
                    }]
                }]
            }]
        }],
        "labs":[{
            "missing_field": "missing_value"
        }]
    }],
    "Asthma":[{}]
}]

}

But I have no idea what the right way to do this should be. Should I just be making javascript objects? Does JSON make sense for this project?

What is the correct syntax to set something like this up?

Here is my code so far:

$(document).ready(function() {
    $.getJSON('js/orders.json', function(json) {
      $.each(json.problems, function(index, order) {
        $('.loadMeds').append('<p>' + order.name + '</p>')
      });
    });
});

解决方案

The first code is an example of Javascript code, which is similar, however not JSON. JSON would not have 1) comments and 2) the var keyword

You don't have any comments in your JSON, but you should remove the var and start like this:

orders: {

The [{}] notation means "object in an array" and is not what you need everywhere. It is not an error, but it's too complicated for some purposes. AssociatedDrug should work well as an object:

"associatedDrug": {
                "name":"asprin",
                "dose":"",
                "strength":"500 mg"
          }

Also, the empty object labs should be filled with something.

Other than that, your code is okay. You can either paste it into javascript, or use the JSON.parse() method, or any other parsing method (please don't use eval)

Update 2 answered:

obj.problems[0].Diabetes[0].medications[0].medicationsClasses[0].className[0].associatedDrug[0].name

returns 'aspirin'. It is however better suited for foreaches everywhere

这篇关于对象和数组的复杂的嵌套JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆