在Jquery中读取JSON结果的值

时间:2022-10-26 10:27:36

Here is my output of WebMethod through Ajax call:

这是我通过Ajax调用输出的WebMethod:

 var item=""[{\"Column1\":\"false\"}]""

There is always one row output,i-e true or false,i want to get the value of Column1,i already try Jquery.ParseJson(item),but it gives Illegal Token o error,Kindly help me how to read this value.Kindly check the inverted commas,this is the exact outcome of my web method, and this outcome and format is a necessary condition of scenario.Thanks.On using loop it gives the error: 在Jquery中读取JSON结果的值

总有一行输出,即true或false,我想获取Column1的值,我已经尝试过Jquery.ParseJson(item),但它给出了Illegal Token o error,请帮我如何读取这个值。请检查引号,这是我的Web方法的确切结果,这个结果和格式是scenario的必要条件。谢谢。使用循环它给出错误:

3 个解决方案

#1


0  

I understand that above solutions not work perfectly with your browsers, here is another alternate solution, though I know that it may not fit your scenario, but as your output is either true or false .Instead of using JsonConvert on server end, simply return the object array to client end and read value like this.

我知道上面的解决方案不能与您的浏览器完美配合,这是另一种替代解决方案,虽然我知道它可能不适合您的场景,但是因为您的输出是真或假。而不是在服务器端使用JsonConvert,只需返回对象数组到客户端并读取这样的值。

var tempo=item[0].Column1;

#2


2  

If I understand your problem correctly, I think your extra quotes around the strings are a problem, this is invalid syntax.

如果我正确理解你的问题,我认为你的字符串附加引号是一个问题,这是无效的语法。

This works:

这有效:

var item = "[{\"Column1\":\"false\"}]";
var parsed = JSON.parse(item);
parsed.forEach(function(row) {
    console.log(row.Column1);
});
console.log(parsed[0].Column1);

Here is a jsfiddle.

这是一个jsfiddle。

See here about jQuery.ParseJson vs JSON.parse, I prefer JSON.parse, but either should work fine.

看到这里关于jQuery.ParseJson vs JSON.parse,我更喜欢JSON.parse,但要么都应该正常工作。

In the case of older browsers without forEach use a for loop or a library like underscore.

对于没有forEach的旧浏览器,使用for循环或像下划线这样的库。

var item="[{\"Column1\":\"false\"}]";
var parsed = JSON.parse(item);
//if forEach is not supported:
for (var i = 0; i < parsed.length; i++) {
    console.log(parsed[i].Column1);
}
console.log(parsed[0].Column1);

Here is a for loop jsfiddle.

这是一个for循环jsfiddle。

#3


0  

Not sure about the output of your service but I think you could try this:

不确定您的服务输出,但我认为您可以尝试这样做:

str = 'var item=""[{\"Column1\":\"false\"}]""';
str = str.replace(/"/g, '');//remove quotes and slashes to make valid json
eval(str);//evaluate the string
console.log(item[0].Column1);

#1


0  

I understand that above solutions not work perfectly with your browsers, here is another alternate solution, though I know that it may not fit your scenario, but as your output is either true or false .Instead of using JsonConvert on server end, simply return the object array to client end and read value like this.

我知道上面的解决方案不能与您的浏览器完美配合,这是另一种替代解决方案,虽然我知道它可能不适合您的场景,但是因为您的输出是真或假。而不是在服务器端使用JsonConvert,只需返回对象数组到客户端并读取这样的值。

var tempo=item[0].Column1;

#2


2  

If I understand your problem correctly, I think your extra quotes around the strings are a problem, this is invalid syntax.

如果我正确理解你的问题,我认为你的字符串附加引号是一个问题,这是无效的语法。

This works:

这有效:

var item = "[{\"Column1\":\"false\"}]";
var parsed = JSON.parse(item);
parsed.forEach(function(row) {
    console.log(row.Column1);
});
console.log(parsed[0].Column1);

Here is a jsfiddle.

这是一个jsfiddle。

See here about jQuery.ParseJson vs JSON.parse, I prefer JSON.parse, but either should work fine.

看到这里关于jQuery.ParseJson vs JSON.parse,我更喜欢JSON.parse,但要么都应该正常工作。

In the case of older browsers without forEach use a for loop or a library like underscore.

对于没有forEach的旧浏览器,使用for循环或像下划线这样的库。

var item="[{\"Column1\":\"false\"}]";
var parsed = JSON.parse(item);
//if forEach is not supported:
for (var i = 0; i < parsed.length; i++) {
    console.log(parsed[i].Column1);
}
console.log(parsed[0].Column1);

Here is a for loop jsfiddle.

这是一个for循环jsfiddle。

#3


0  

Not sure about the output of your service but I think you could try this:

不确定您的服务输出,但我认为您可以尝试这样做:

str = 'var item=""[{\"Column1\":\"false\"}]""';
str = str.replace(/"/g, '');//remove quotes and slashes to make valid json
eval(str);//evaluate the string
console.log(item[0].Column1);