Reputation: 873
Here is my output of WebMethod through Ajax call:
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:
Upvotes: 2
Views: 71
Reputation: 418
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.
var tempo=item[0].Column1;
Upvotes: 0
Reputation: 1
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);
Upvotes: 0
Reputation: 71810
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.
See here about jQuery.ParseJson vs JSON.parse, I prefer JSON.parse, but either should work fine.
In the case of older browsers without forEach
use a for
loop or a library like underscore.
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.
Upvotes: 2