简体   繁体   English

遍历嵌套的JSON对象

[英]Iterate through nested JSON objects

Using Edmunds API (edmunds.mashery.io), I am trying to parse a car object. 我正在尝试使用Edmunds API(edmunds.mashery.io)解析汽车对象。
As you can see the JSON Response is well formatted into the following nested tree: Make (id, name, models) --> Models (id, name, nicename, styles) --> Styles 如您所见,JSON响应格式良好,格式化为以下嵌套树:Make(id,name,models)-> Models(id,name,nicename,styles)->样式

With this in mind, consider the following API response: 考虑到这一点,请考虑以下API响应:

{
"name": "BMW",
"model": [
    {
        "id": "BMW_2_Series",
        "name": "2 Series",
        "nicename": "2-series",
        "styles": [
            {
                "id": 200744428,
                "name": "228i 2dr Coupe (2.0L 4cyl Turbo 6M)",
                "drivenwheels": "rear wheel drive",
                "numofdoors": 2,
                "manufacturercode": "162A",
                "engine": 200744477,
                "transmission": 200744479,
                "trim": "228i",
                "pricebasemsrp": "32850.0",
                "pricebaseinvoice": "30720.0",
                "pricedeliverycharges": "995.0",
                "submodelbody": "Coupe",
                "submodelname": "2 Series Coupe",
                "submodelnicename": "coupe",
                "mpghighway": "34",
                "mpgcity": "22",
                "categoriesmarket": "Luxury,Performance",
                "epaclass": "Subcompact Cars",
                "vehiclesize": "Compact",
                "crossover": "NA",
                "primarybodytype": "Car"
            },
            {
                "id": 200744429,
                "name": "228i SULEV 2dr Coupe (2.0L 4cyl Turbo 8A)",
                "drivenwheels": "rear wheel drive",
                "numofdoors": 2,
                "manufacturercode": "162B",
                "engine": 200744475,
                "transmission": 200744478,
                "trim": "228i SULEV",
                "pricebasemsrp": "32850.0",
                "pricebaseinvoice": "30720.0",
                "pricedeliverycharges": "995.0",
                "submodelbody": "Coupe",
                "submodelname": "2 Series Coupe",
                "submodelnicename": "coupe",
                "mpghighway": "35",
                "mpgcity": "23",
                "categoriesmarket": "Luxury,Performance",
                "epaclass": "Subcompact Cars",
                "vehiclesize": "Compact",
                "crossover": "NA",
                "primarybodytype": "Car"
            }]},
    {
        "id": "BMW_3_Series",
        "name": "3 Series",
        "nicename": "3-series",
        "styles": [
            {
                "id": 200729833,
                "name": "320i 4dr Sedan (2.0L 4cyl Turbo 8A)",
                "drivenwheels": "rear wheel drive",
                "numofdoors": 4,
                "manufacturercode": "16TI",
                "engine": 200741606,
                "transmission": 200741614,
                "trim": "320i",
                "pricebasemsrp": "33150.0",
                "pricebaseinvoice": "31000.0",
                "pricedeliverycharges": "995.0",
                "submodelbody": "Sedan",
                "submodelname": "3 Series Sedan",
                "submodelnicename": "sedan",
                "mpghighway": "36",
                "mpgcity": "24",
                "categoriesmarket": "Luxury",
                "epaclass": "Compact Cars",
                "vehiclesize": "Midsize",
                "crossover": "NA",
                "primarybodytype": "Car"
            }
       }
}  

Using JQuery's ajax, I am able to load the JSON response. 使用JQuery的ajax,我能够加载JSON响应。 I am trying to create dynamic buttons based on the Model's id (Each model is a boxed container containing information about the model populated from this response. 我正在尝试根据模型的ID创建动态按钮(每个模型是一个盒装容器,其中包含有关从此响应中填充的模型的信息。

My current issue stands that when I try to iterate JQuery's .each, I get: 我当前的问题是,当我尝试迭代JQuery的.each时,我得到:
key: model 关键:型号
value: [Object] [Object] ... [Object] 值:[对象] [对象] ... [对象]

What I'm trying to accomplish is accessing the Object's content in a dynamic way and populate to populate it. 我要完成的工作是以动态方式访问对象的内容并进行填充。

In addition to the scope of this question, I have a more general question based on the BIG Picture: 除了这个问题的范围之外,我还有一个基于BIG Picture的更一般的问题:

Using Edmund's API I plan on making a car selection website using buttons where: 我计划使用Edmund的API使用以下按钮创建汽车选择网站:
State 1: Makes populated, when make is selected, go to state 2. 状态1:制造商填充,选择制造商后,进入状态2。
State 2: Model buttons are populated from JSON API, move to state 3. 状态2:从JSON API填充了模型按钮,然后转到状态3。

Here is my current jQuery function: 这是我当前的jQuery函数:

$.getJSON('http://127.0.0.1:8000/makes/200000081.json', function( models ) {
      console.log(models.model[0].name)

  });

Here is my output: 这是我的输出:

2 Series 2系列

If I change model[0] to model[3] the result changes (as it should.) How can I get model[x].name in an array I can manipulate with HTML? 如果将model [0]更改为model [3],结果会发生变化(应如此)。如何在可以用HTML处理的数组中获取model [x] .name?

Maybe something like this could help. 也许这样的事情可能会有所帮助。

 var response = { "name": "BMW", "model": [ { "id": "BMW_2_Series", "name": "2 Series", "nicename": "2-series", "styles": [ { "id": 200744428, "name": "228i 2dr Coupe (2.0L 4cyl Turbo 6M)", "drivenwheels": "rear wheel drive", "numofdoors": 2, "manufacturercode": "162A", "engine": 200744477, "transmission": 200744479, "trim": "228i", "pricebasemsrp": "32850.0", "pricebaseinvoice": "30720.0", "pricedeliverycharges": "995.0", "submodelbody": "Coupe", "submodelname": "2 Series Coupe", "submodelnicename": "coupe", "mpghighway": "34", "mpgcity": "22", "categoriesmarket": "Luxury,Performance", "epaclass": "Subcompact Cars", "vehiclesize": "Compact", "crossover": "NA", "primarybodytype": "Car" }, { "id": 200744429, "name": "228i SULEV 2dr Coupe (2.0L 4cyl Turbo 8A)", "drivenwheels": "rear wheel drive", "numofdoors": 2, "manufacturercode": "162B", "engine": 200744475, "transmission": 200744478, "trim": "228i SULEV", "pricebasemsrp": "32850.0", "pricebaseinvoice": "30720.0", "pricedeliverycharges": "995.0", "submodelbody": "Coupe", "submodelname": "2 Series Coupe", "submodelnicename": "coupe", "mpghighway": "35", "mpgcity": "23", "categoriesmarket": "Luxury,Performance", "epaclass": "Subcompact Cars", "vehiclesize": "Compact", "crossover": "NA", "primarybodytype": "Car" }]}, { "id": "BMW_3_Series", "name": "3 Series", "nicename": "3-series", "styles": [ { "id": 200729833, "name": "320i 4dr Sedan (2.0L 4cyl Turbo 8A)", "drivenwheels": "rear wheel drive", "numofdoors": 4, "manufacturercode": "16TI", "engine": 200741606, "transmission": 200741614, "trim": "320i", "pricebasemsrp": "33150.0", "pricebaseinvoice": "31000.0", "pricedeliverycharges": "995.0", "submodelbody": "Sedan", "submodelname": "3 Series Sedan", "submodelnicename": "sedan", "mpghighway": "36", "mpgcity": "24", "categoriesmarket": "Luxury", "epaclass": "Compact Cars", "vehiclesize": "Midsize", "crossover": "NA", "primarybodytype": "Car" } ] } ] } var model = response.model; var brandname = response.name; $(model).each(function(){ var series = $(this).get(0); $("#view").append("<button id=\\""+series.id+"\\" name=\\""+series.id+"\\">"+brandname+" "+series.name+"</button>"); $(document).on("click","#"+series.id,function() { $(series.styles).each(function(){ var make = $(this).get(0); if($("#"+make.id).length<=0) { $("#subview").append("<button id=\\""+make.id+"\\" name=\\""+make.id+"\\">"+make.name+"</button>"); } else { $("#"+make.id).remove(); } $(document).on("click","#"+make.id,function() { var carinfo = ""; for (var prop in make) { carinfo += prop+' = '+make[prop]+"\\n"; } alert(carinfo); }); }); }); }); 
 #view button,#subview button{ width:100%; display:block; padding:2rem; } #view button { color:#fff; background-color:#326d1a; } #subview button{ color:#fff; background-color:#a22fa1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="view"></div> <div id="subview"></div> 

I was looking for a code snippet like this: 我正在寻找这样的代码片段:

$.getJSON('http://127.0.0.1:8000/makes/' + make_id + '.json', function( models     ) {
       $(models.model).each(function(){
           console.log(this.name + ' ' + this.id)
       });

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

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