您好我从服务器端返回了以下JSON:
{"command":"SELECT","rowCount":1,"oid":null,"rows":[{"username":"xxxx"}],"fields":[{"name":"username","tableID":34722,"columnID":3,"dataTypeID":1043,"dataTypeSize":-1,"dataTypeModifier":204,"format":"text"}],"_parsers":[null]}
我在javascript中解析json,如下所示:
var ParsedJSONResponse = $.parseJSON(JSONResponseFromServerSide);
变量"ParsedJSONResponse"
总是为null,我的JSON有效我在JSONLint中检查了它,那么结果是什么?
答案 0 :(得分:3)
您应该解析字符串
var stringJson = '{"command":"SELECT","rowCount":1,"oid":null,"rows":[{"username":"xxxx"}],"fields":[{"name":"username","tableID":34722,"columnID":3,"dataTypeID":1043,"dataTypeSize":-1,"dataTypeModifier":204,"format":"text"}],"_parsers":[null]}';
var ParsedJSONResponse = $.parseJSON(stringJson);
你只有一个Json(JSONResponseFromServerSide
),没有理由解析它。
解析Json对象返回null
。
$.parseJSON({}); // returns `null`
答案 1 :(得分:0)
您的代码是正确的,请查看此JSFiddle。
var JSONResponseFromServerSide = '{"command":"SELECT","rowCount":1,"oid":null,"rows":[{"username":"xxxx"}],"fields":[{"name":"username","tableID":34722,"columnID":3,"dataTypeID":1043,"dataTypeSize":-1,"dataTypeModifier":204,"format":"text"}],"_parsers":[null]}'; //it's your json string
$.parseJSON(JSONResponseFromServerSide); //nothing wrong
如果JSONResponseFromServerSide
已经是JavaScript对象,那么您不必执行parseJSON