Jquery JSON 解析返回未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14183676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery JSON parse returns undefined
提问by user1172682
I'm trying to parse a JSON string but when I do I get undefined.
我正在尝试解析一个 JSON 字符串,但是当我解析时却没有定义。
var codes = jQuery.parseJSON(response);
$.each(codes, function (key, value) {
alert(value.Display);
});
Here is the contents of codes
variable above:
这是codes
上面变量的内容:
["{ Display = string1, Sell = string2 }",
"{ Display = string1, Sell = string2 }"]
alert returns value.Display as undefined
. I expected "String1". What am I doing wrong?
警报返回值undefined
。显示为。我期待“String1”。我究竟做错了什么?
回答by
That's not a valid JSON string.
A correct string would look like this:
这不是有效的 JSON 字符串。
正确的字符串如下所示:
'{ "Display": "string1", "Sell": "string2" }'
回答by Guffa
You can't. There is no Display
property in the array, it's an array containing two strings.
你不能。Display
数组中没有属性,它是一个包含两个字符串的数组。
The strings are similar to JSON, but not enough to be parsed.
字符串类似于 JSON,但不足以解析。
If you make the strings follow the JSON standard, you can parse each item in the array into an object, then you can access the Display
property:
如果让字符串遵循JSON标准,则可以将数组中的每一项解析为一个对象,然后就可以访问该Display
属性:
var response = '["{ \"Display\": \"string1\", \"Sell\": \"string2\" }", "{ \"Display\": \"string1\", \"Sell\": \"string2\" }"]';
var codes = jQuery.parseJSON(response);
$.each(codes, function (key, value) {
var obj = jQuery.parseJSON(value);
alert(obj.Display);
});
Demo: http://jsfiddle.net/Guffa/wHjWf/
演示:http: //jsfiddle.net/Guffa/wHjWf/
Alternatively, you can make the entire input follow the JSON standard, so that you can parse it into an array of objects:
或者,您可以使整个输入遵循 JSON 标准,以便您可以将其解析为对象数组:
var response = '[{ "Display": "string1", "Sell": "string2" }, { "Display": "string1", "Sell": "string2" }]';
var codes = jQuery.parseJSON(response);
console.log(codes);
$.each(codes, function (key, value) {
alert(value.Display);
});
回答by Eric Leschinski
I got this error by accidentally json-encoding my array data twice.
我不小心对我的数组数据进行了两次 json 编码,从而得到了这个错误。
Like this:
像这样:
$twicefail = '{"penguins" : "flipper"}';
return json_encode( $twicefail );
Then in my view, I picked it up like this:
然后在我看来,我是这样捡起来的:
var json_data = jQuery.parseJSON(my_json_response);
alert(json_data.penguins); //Here json_data.penguins is undefined because I
//json_encoded stuff that was already json.
Corrected code follows:
更正后的代码如下:
$twicefail = '{"penguins" : "flipper"}';
return $twicefail;
Then in my view, pick it up like this:
然后在我看来,像这样捡起来:
var json_data = jQuery.parseJSON(my_json_response);
alert(json_data.penguins); //json_data.penguins has value 'flipper'.
回答by mohabduls
You can easy try this:
你可以很容易地试试这个:
var json = jQuery.parseJSON(response);
//get value from response of json
var Display = json[0].Display;
All you need is just the [0] for specify of data because this is default parameter (unset).
It works for me!
您只需要 [0] 来指定数据,因为这是默认参数(未设置)。
这个对我有用!