从JSON响应中解析数据

时间:2016-04-27 10:20:01

标签: javascript jquery

我有一个JSON响应数据如下

{"response":"9",
 "status":"Success",
 "msg":"Valid Access",
 "data":[{"id":"1","title":"A"},
        {"id":"2","title":"B"},
        {"id":"3","title":"C"}]
 }

如何使用jquery获取数据数组?

在parse / stringify

中,在控制台中将错误显示为parsererror; SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 666 of the JSON data

4 个答案:

答案 0 :(得分:4)

使用点符号

var obj = {"response":"9",
 "status":"Success",
 "msg":"Valid Access",
 "data":[{"id":"1","title":"A"},
        {"id":"2","title":"B"},
        {"id":"3","title":"C"}]
 }

console.log(obj.data);

https://jsfiddle.net/c8z35au4/

或解析数据

var unparsed = '{"response":"9",
     "status":"Success",
     "msg":"Valid Access",
     "data":[{"id":"1","title":"A"},
            {"id":"2","title":"B"},
            {"id":"3","title":"C"}]
     }';
var obj = JSON.stringify(unparsed);
console.log(obj.data);

答案 1 :(得分:3)

如果是Ajax请求:

$.ajax({
    dataType: 'json',
    success: function (response) {
        console.log(response.data);
    }
});

如果是字符串,请使用var response = JSON.parse(string)response.data

答案 2 :(得分:2)



var data = {"response":"9",
 "status":"Success",
 "msg":"Valid Access",
 "data":[{"id":"1","title":"A"},
        {"id":"2","title":"B"},
        {"id":"3","title":"C"}]
 }
 
 
 
 console.log(JSON.stringify(data.data))




答案 3 :(得分:-1)

最好的方法是

 var data = {"response":"9",
     "status":"Success",
     "msg":"Valid Access",
     "data":[{"id":"1","title":"A"},
            {"id":"2","title":"B"},
            {"id":"3","title":"C"}]
     }

    data=JSON.parse(data);
   var requiredData=data.data;
   for(var i in requiredData)
{
     var id = requiredData[i].id;
     var title = requiredData[i].title;
     console.log(id);
     console.log(title);

}