0

您好,我从服务器端返回了以下 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" 始终为空,我的 JSON 有效我在 JSONLint 中检查了它,请问发生了什么?

4

2 回答 2

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`  
于 2013-09-19T17:12:20.523 回答
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

于 2013-09-19T17:10:56.073 回答