I made a ajax request to server and get back response from as json string.when i go for JSON.stringify, it having lots of white space in response.when i try to parse on json object received error messgae SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.
我向服务器发出了一个ajax请求,并从json string中获取响应。当我去JSON.stringify时,它有很多空白响应。当我尝试解析json对象时收到错误messgae SyntaxError:JSON.parse: JSON数据的第1行第1列的意外字符。
Below is the sample code:
以下是示例代码:
$.post("http://example.com/index.cfm?fuseaction=shopping.admin&stamps=getStampsRates&Order_No="+order_id+"&stamps_service_type="+selectedServiceType+"",function(data,status)
{
if(status=="success")
{
var data=JSON.parse(data);
//original resonse aspected from server
// var json ='[{"PACKAGETYPE":"Postcard","AMOUNT":0.34},{"PACKAGETYPE":"Letter","AMOUNT":0.48},{"PACKAGETYPE":"Large Envelope or Flat","AMOUNT":0.98},{"PACKAGETYPE":"Thick Envelope","AMOUNT":1.93},{"PACKAGETYPE":"Package","AMOUNT":1.93},{"PACKAGETYPE":"Large Package","AMOUNT":1.93}] ';
$.each(data, function(idx, obj) {
alert(obj.PACKAGETYPE);
});
}
});
I have made few changes then tried to parse:
我做了一些改动然后尝试解析:
var data=JSON.stringify(data);
var newJ= JSON.parse(data);
alert("newJ:"+JSON.stringify(newJ));
and getting following resonse :
并获得以下共鸣:
"\r\n\r\n\t\r\n[{\"PACKAGETYPE\":\"Large Envelope or Flat\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Thick Envelope\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Package\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Large Package\",\"AMOUNT\":2.69}]\r\n\t\r\n\r\n"
And try to iterated above json object getting error TypeError: t is undefined.
并尝试迭代上面的json对象获取错误TypeError:t未定义。
Please help me to solve above problem.
请帮我解决上面的问题。
Thanks
1 个解决方案
#1
1
I think your issue may be compound. First, in the same scope you have two 'data' which may overwrite the original response because of reference issue. Another, like @adeneo said, the json was malformed from the server side. So try change to this. I created a JSFiddle to parse both versions of your JSON responses since we are not certain exactly how the data response looks like based on your post.
我认为你的问题可能很复杂。首先,在同一范围内,您有两个“数据”,由于引用问题,可能会覆盖原始响应。另一个像@adeneo说的那样,json在服务器端出现了畸形。所以试着改变这个。我创建了一个JSFiddle来解析你的JSON响应的两个版本,因为我们根据你的帖子不确定数据响应的确切方式。
Code sample here on JSFiddle
JSFiddle上的代码示例
var json ='[{"PACKAGETYPE":"Postcard","AMOUNT":0.34},{"PACKAGETYPE":"Letter","AMOUNT":0.48},{"PACKAGETYPE":"Large Envelope or Flat","AMOUNT":0.98},{"PACKAGETYPE":"Thick Envelope","AMOUNT":1.93},{"PACKAGETYPE":"Package","AMOUNT":1.93},{"PACKAGETYPE":"Large Package","AMOUNT":1.93}]';
var json1 = '"\r\n\r\n\t\r\n[{\"PACKAGETYPE\":\"Large Envelope or Flat\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Thick Envelope\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Package\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Large Package\",\"AMOUNT\":2.69}]\r\n\t\r\n\r\n"'
var obj = JSON.parse(json);
$.each(obj, function(i, item) {
console.log(obj[i].PACKAGETYPE);
});
json1 = json1.split('[')[1].split(']')[0].replace('\\','');
json1 = '[' + json1 + ']';
console.log(json1);
var obj = JSON.parse(json1);
$.each(obj, function(i, item) {
console.log(obj[i].PACKAGETYPE);
});
#1
1
I think your issue may be compound. First, in the same scope you have two 'data' which may overwrite the original response because of reference issue. Another, like @adeneo said, the json was malformed from the server side. So try change to this. I created a JSFiddle to parse both versions of your JSON responses since we are not certain exactly how the data response looks like based on your post.
我认为你的问题可能很复杂。首先,在同一范围内,您有两个“数据”,由于引用问题,可能会覆盖原始响应。另一个像@adeneo说的那样,json在服务器端出现了畸形。所以试着改变这个。我创建了一个JSFiddle来解析你的JSON响应的两个版本,因为我们根据你的帖子不确定数据响应的确切方式。
Code sample here on JSFiddle
JSFiddle上的代码示例
var json ='[{"PACKAGETYPE":"Postcard","AMOUNT":0.34},{"PACKAGETYPE":"Letter","AMOUNT":0.48},{"PACKAGETYPE":"Large Envelope or Flat","AMOUNT":0.98},{"PACKAGETYPE":"Thick Envelope","AMOUNT":1.93},{"PACKAGETYPE":"Package","AMOUNT":1.93},{"PACKAGETYPE":"Large Package","AMOUNT":1.93}]';
var json1 = '"\r\n\r\n\t\r\n[{\"PACKAGETYPE\":\"Large Envelope or Flat\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Thick Envelope\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Package\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Large Package\",\"AMOUNT\":2.69}]\r\n\t\r\n\r\n"'
var obj = JSON.parse(json);
$.each(obj, function(i, item) {
console.log(obj[i].PACKAGETYPE);
});
json1 = json1.split('[')[1].split(']')[0].replace('\\','');
json1 = '[' + json1 + ']';
console.log(json1);
var obj = JSON.parse(json1);
$.each(obj, function(i, item) {
console.log(obj[i].PACKAGETYPE);
});