2

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: enter image description here

Muhammad Ali
  • 853
  • 1
  • 10
  • 18

3 Answers3

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.

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.

Community
  • 1
  • 1
Bjorn
  • 69,215
  • 39
  • 136
  • 164
  • It looks like your browser does not have `forEach`. You'll have to use `for(;;)` or something like underscore if you want to support an older browser. I will update the answer. – Bjorn Oct 05 '14 at 18:41
  • @Bjron ...i am using Chrome having version 33.0.1750.154 dev-m – Muhammad Ali Oct 05 '14 at 18:42
  • My version of Chrome is Version 37.0.2062.124, I believe that's the most recent stable version. I'd think 33 would have `forEach` it's been around for a while, but I'm not sure. You might just want to use a for loop or a library like underscore/lodash. – Bjorn Oct 05 '14 at 18:44
  • on opera and firefox,it gives the error undefined is not a function – Muhammad Ali Oct 05 '14 at 18:50
  • Did you try it with the `for` loop instead of the `forEach`? – Bjorn Oct 05 '14 at 19:13
  • Maybe you can create a jsfiddle for me to try. – Bjorn Oct 05 '14 at 19:13
  • Yes I try it with for loop – Muhammad Ali Oct 05 '14 at 19:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62487/discussion-between-bjorn-tipling-and-muhammad-ali). – Bjorn Oct 05 '14 at 19:46
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);
robertou
  • 1
  • 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.

var tempo=item[0].Column1;
Karim Musa
  • 378
  • 1
  • 2
  • 9