如何在ajax成功中得到json的回应

时间:2015-07-24 13:15:00

标签: jquery ajax json

{
    "status": {
        "imsg": "Cannot create alignment that already exists",
        "emsg": "Unable to serve your request",
        "category": "Conflict",
        "code": 409
    },
    "resources": [{
        "data": {
            "match_code": "I",
            "standard": {
                "guid": "A44D16E2-8386-11E1-9D3E-8F3A9DFF4B22"
            },
            "self": "http://api.academicbenchmarks.com/rest/v3/standards/A44D16E2-8386-11E1-9D3E-8F3A9DFF4B22/assets
/1351AE36-4B61-11E0-8B12-F25B9DFF4B22",
            "label": "Include",
            "asset": {
                "guid": "1351AE36-4B61-11E0-8B12-F25B9DFF4B22"
            }
        }
    }],
    "count": 1,
    "type": "alignment"
}

我想获得“emsg”和“资源”数据的状态

任何人都建议得到结果

使用以下代码在ajax中接收

 $.ajax({
        type: 'post',
        url: apicall,
        dataType: "json",
        success: function(data) { 
                      alert(data);

        }
    });

3 个答案:

答案 0 :(得分:0)

您需要使用JSON.parse从服务器解码您的JSON。像这样。

var b='{"status":{"imsg": "Cannot create alignment that already exists", "emsg": "Unable to serve your request", "category": "Conflict", "code": 409}, "resources": [{"data":{"match_code": "I", "standard":{"guid": "A44D16E2-8386-11E1-9D3E-8F3A9DFF4B22"}, "self": "http://api.academicbenchmarks.com/rest/v3/standards/A44D16E2-8386-11E1-9D3E-8F3A9DFF4B22/assets/1351AE36-4B61-11E0-8B12-F25B9DFF4B22", "label": "Include", "asset":{"guid": "1351AE36-4B61-11E0-8B12-F25B9DFF4B22"}}}], "count": 1, "type": "alignment"}';

var b1=JSON.parse(b);
console.log(b1);
console.log(b1['status']['emsg']);
console.log(b1['resources']);

小提琴:http://jsfiddle.net/d5xzexw9/6/

像这样访问值,

var b1=JSON.parse(b); 
var em=b1['status']['emsg']; 
var label1=b1['resources']["0"]["data"]["label"]; //AND SO ON...

答案 1 :(得分:0)

试试这个:

data.status.emsg //to get value of emsg and 
data.resources //to get resources. 

Data.resources会返回一个数组

答案 2 :(得分:0)

试试这个

$.ajax({
        type: 'post',
        url: apicall,
        dataType: "json",
        success: function(data) { 
         alert(data.status.emsg);
        }
    });