简体   繁体   English

从嵌套响应中获取 json 值

[英]Getting json value from nested response

I have the following json return.我有以下 json 返回。 I wish to get all the value in car, car.Make and car.Price.我希望获得 car、car.Make 和 car.Price 的所有价值。 How can it be done?怎么做到呢? I have tried this but not working我试过这个但没有用

 $.each(jqXHR.responseJSON.ModelState(index, value), function() { alert(jqXHR.responseJSON.ModelState[index].value); });

在此处输入图片说明

 { "Message": "The request is invalid.", "ModelState": { "car": [ "Required property 'Make' not found in JSON. Path '', line 1, position 57." ], "car.Make" : [ "The Make field is required." ], "car.Price": [ "The field Price must be between 0 and 200000." ] } }

You would get the 3 values like this:你会得到这样的 3 个值:

alert(json.ModelState["car"]);
alert(json.ModelState["car.Make"]);
alert(json.ModelState["car.Price"]);

this will also work for car这也适用于汽车

alert(json.ModelState.car);

Since the other properties contain a '.'由于其他属性包含一个 '.' in the name then it looks for a property Make on property car rather than property called car.Make therefore we need to use the string key.在名称中,它会在属性 car 上查找属性 Make 而不是名为 car.Make 的属性,因此我们需要使用字符串键。

Notice in this fiddle the last two are undefined.请注意,在这个小提琴中,最后两个是未定义的。 Best not to use '.'最好不要使用'.' property names if going to be used in json.如果要在 json 中使用,则属性名称。

https://jsfiddle.net/1zgybf9m/ https://jsfiddle.net/1zgybf9m/

To print all data in modelState and all cars, assuming you can have more than one.要打印 modelState 和所有汽车中的所有数据,假设您可以拥有多个。 IT would be more like this:它会更像这样:

https://jsfiddle.net/1zgybf9m/1/ https://jsfiddle.net/1zgybf9m/1/

var json = {
    "Message": "The request is invalid.",
    "ModelState": { 
        "car": [
            "1 Required property 'Make' not found in JSON. Path '', line 1, position 57.",
            "2 Required property 'Make' not found in JSON. Path '', line 1, position 57."
        ],
        "car.Make" : [
            "1 The Make field is required.",
            "2 The Make field is required."
        ], 
        "car.Price": [
            "1 The field Price must be between 0 and 200000.",
            "2 The field Price must be between 0 and 200000."
        ]
    }
}

for(var prop in json.ModelState){
    console.log(prop);
    for(var value in json.ModelState[prop]){
       console.log(json.ModelState[prop][value]);
    }
}

Having a variable with the JSON:有一个带有 JSON 的变量:

var obj= {

        "Message": "The request is invalid.",
        "ModelState": { 
            "car": [
                "Required property 'Make' not found in JSON. Path '', line 1, position 57."
            ],
            "car.Make" : [
                "The Make field is required."
            ], 
            "car.Price": [
                "The field Price must be between 0 and 200000."
            ]
        }
    };

For loop access through ModelState:通过 ModelState 进行循环访问:

for (var prop in obj.ModelState){
    for (var i=0; i<obj.ModelState[prop].length; i++){
        console.log(obj.ModelState[prop][i]);    
    }    
}

fiddle: http://jsfiddle.net/m8gq0Lew/2/小提琴: http : //jsfiddle.net/m8gq0Lew/2/

It is also recommended not to use .也建议不要使用. in JSON-keys在 JSON 键中

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

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