3

How to parse these JSon array am getting from json_encode(arr), the output is:

[{"id":"44","data":"[[10],[27],[13]]","_types":"ff"},  {"id":"44","data":"[[140],[327],[213]]","_types":"44f"}]

I need to iterate this using java script and fetch each values. Am getting an error, Unexpected token.

7
  • JSON.parse(inputStr) should do Commented Feb 22, 2016 at 7:15
  • If json.parse() doesnt work, please show us your code where you try to fetch Commented Feb 22, 2016 at 7:18
  • Your question is not specific enough. What do you want to do (example)? Commented Feb 22, 2016 at 7:18
  • When i do json.parse am getting Uncaught SyntaxError: Unexpected token N.I need to fetch each user ids and data from this json. Commented Feb 22, 2016 at 7:22
  • can you share a fiddle jsfiddle.net? Commented Feb 22, 2016 at 7:23

2 Answers 2

2

You are getting a string, you simply need to do JSON.parse

var str = '[{"id":"44","data":"[[10],[27],[13]]","_types":"ff"},  {"id":"44","data":"[[140],[327],[213]]","_types":"44f"}]';

var obj = JSON.parse( str );

alert( obj[0].id );
Sign up to request clarification or add additional context in comments.

4 Comments

there is no single codes in my json_enode($arr) output.Am getting this error Uncaught TypeError: Cannot use 'in' operator when i tried this
@user263371 my code doesn't have any in operator. I wonder why got the error you got. Can you share a fiddle? Or can you update the OP to show the code that you have tried?
the code you have provided is working, but the json assigned to str is in single codes right?my json output doesnt has single codes i cannot apply json.parse to str..
@user263371 can you share the exact JSON string you are testing with in your environment?
1

You are getting array of java script object,i think you don't need to parse this.Just iterate this and fetch the result like below.

var q = [{"id":"44","data":"[[10],[27],[13]]","_types":"ff"},  {"id":"44","data":"[[140],[327],[213]]","_types":"44f"}];

 q.forEach(function(i){console.log(i.id)});

1 Comment

Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.