I have an AJAX call, which calls a controller. This controller returns the following JSON:
我有一个AJAX调用,它调用一个控制器。该控制器返回以下JSON:
{"officeProducts":"[{\"Id\":96,\"MyProperty\":null,\"Enabled\":true,\"Envelope\":{\"Id\":1,\"Quality\":\"God\",\"PaperSize\":\"A4\",\"Type\":\"Window\"}},{\"Id\":169,\"MyProperty\":null,\"Enabled\":true,\"Envelope\":{\"Id\":1,\"Quality\":\"God\",\"PaperSize\":\"A4\",\"Type\":\"Window\"}},{\"Id\":174,\"MyProperty\":null,\"Enabled\":true,\"Envelope\":{\"Id\":1,\"Quality\":\"God\",\"PaperSize\":\"A4\",\"Type\":\"Window\"}},{\"Id\":175,\"MyProperty\":null,\"Enabled\":true,\"Envelope\":{\"Id\":1,\"Quality\":\"God\",\"PaperSize\":\"A4\",\"Type\":\"Window\"}}]"}
And now I want to iterate the list. So I basically want to iterate the officeProducts
.
现在我想迭代列表。所以我基本上想要迭代officeProducts。
I have the following code, where I obviously do something wrong, as I get a:
我有以下代码,我显然做错了什么,因为我得到了:
Error:
错误:
Error: Syntax error, unrecognized expression: [{"Id":96,"MyProperty":null,"Enabled":true,"Envelope":{"Id":1,"Quality":"God","PaperSize":"A4","Type":"Window"}},{"Id":169,"MyProperty":null,"Enabled":true,"Envelope":{"Id":1,"Quality":"God","PaperSize":"A4","Type":"Window"}},{"Id":174,"MyProperty":null,"Enabled":true,"Envelope":{"Id":1,"Quality":"God","PaperSize":"A4","Type":"Window"}},{"Id":175,"MyProperty":null,"Enabled":true,"Envelope":{"Id":1,"Quality":"God","PaperSize":"A4","Type":"Window"}}]
My AJAX call:
我的AJAX电话:
self.updateOfficeProducts = function() {
$.ajax({
url: '/SingleLetter/GetOfficeProducts',
type: 'POST',
data: {
'country': self.countryId
},
dataType: 'json',
success: function (data) {
console.log(data.officeProducts);
$(data.officeProducts).each(function (index, ele) {
alert(ele.Id);
});
}
});
};
So I would expect to iterate 4 different objects, where I can say things like ele.Id
or ele.Enabled
. Instead I get my syntax error.
所以我希望迭代4个不同的对象,我可以说ele.Id或ele.Enabled之类的东西。相反,我得到了我的语法错误。
What am I doing wrong? :-) Obviously some syntax thing.
我究竟做错了什么? :-)显然有些语法的东西。
1 个解决方案
#1
4
Use JSON.parse-
使用JSON.parse-
success: function (data) {
data = JSON.parse(data);
$(data.officeProducts).each(function (index, ele) {
alert(ele.Id);
});
...
Edit
编辑
Another thing — I'm not sure of .NET, but the response you're getting back is not of the form expected by ajax. The string is not properly json-encoded.
另一件事 - 我不确定.NET,但你回复的反应不是ajax预期的形式。该字符串未正确进行json编码。
You have to remove "
before and after the [
and ]
, so the string would be-
你必须删除“[和]之前和之后,所以字符串将是 -
var a = '{"officeProducts": [{\"Id\":96,\"MyProperty\":null,\"Enabled\":true,\"Envelope\":{\"Id\":1,\"Quality\":\"God\",\"PaperSize\":\"A4\",\"Type\":\"Window\"}}] }';
// ^here and here^
的jsfiddle
Hope that helps.
希望有所帮助。
#1
4
Use JSON.parse-
使用JSON.parse-
success: function (data) {
data = JSON.parse(data);
$(data.officeProducts).each(function (index, ele) {
alert(ele.Id);
});
...
Edit
编辑
Another thing — I'm not sure of .NET, but the response you're getting back is not of the form expected by ajax. The string is not properly json-encoded.
另一件事 - 我不确定.NET,但你回复的反应不是ajax预期的形式。该字符串未正确进行json编码。
You have to remove "
before and after the [
and ]
, so the string would be-
你必须删除“[和]之前和之后,所以字符串将是 -
var a = '{"officeProducts": [{\"Id\":96,\"MyProperty\":null,\"Enabled\":true,\"Envelope\":{\"Id\":1,\"Quality\":\"God\",\"PaperSize\":\"A4\",\"Type\":\"Window\"}}] }';
// ^here and here^
的jsfiddle
Hope that helps.
希望有所帮助。