0

I am trying to parse json source with javascript.

I have this json source:

var exam = '{
    status : 'connected',
    authResponse: {
    accessToken: 'it is accessToken',
    expiresIn:'2014-06-19',
    signedRequest:'it is signedRequest',
    userID:'mp172'
    }
}';

To parse, I use JSON.parse(exam);

After this source, it is not working. I want to parse this source with javascript.

Farax
  • 1,447
  • 3
  • 20
  • 37
  • While JSON and javascript object literal syntax look similar they're not the same. For example you cannot have functions in JSON, you must quote strings with `"` etc. See JSON.org for the specification of the JSON format. – slebetman Jun 18 '14 at 04:01
  • ate you sure you are getting that response as a JSON source @user3750753 – Runcorn Jun 18 '14 at 04:04

5 Answers5

1

Actually, your json source is not valid. According to JSON.org, member should be quote by "

Change exam to {"status":"connected","authResponse":{"accessToken":"it is accessToken","expiresIn":"2014-06-19","signedRequest":"it is signedRequest","userID":"mp172"}}

comphilip
  • 500
  • 5
  • 15
  • Thank you answering my question, actually it is provided from facebook. i can't edit this source. and i am testing facebook development now. i know the syntax... please help me. – user3750753 Jun 18 '14 at 04:01
  • So the source returned by facebook is not for JSON parser, but javascript. Try `eval('return ' + exam);` – comphilip Jun 18 '14 at 04:04
  • Sorry for my eval is wrong. Use `eval("var exam={status:'connected',authResponse:{accessToken:'it is accessToken',expiresIn:'2014-06-19',signedRequest:'it is signedRequest',userID:'mp172'}}")` – comphilip Jun 18 '14 at 04:19
1

Take a look at JSON.stringify and JSON.parse.

var exam = {status : 'connected', authResponse: { accessToken: 'it is accessToken', expiresIn:'2014-06-19', signedRequest:'it is signedRequest', userID:'mp172' }};
// stringify the response first
stringify = JSON.stringify(exam);
// stringified result
console.log(stringify);
// parse the json
final = JSON.parse(stringify);
// parsed final result
console.log(final);

Here is the jsfiddle example

nyzm
  • 2,787
  • 3
  • 24
  • 30
0

Your JSON is invalid , it should look something like this ,

{
    "status": "connected",
    "authResponse": {
        "accessToken": "itisaccessToken",
        "expiresIn": "2014-06-19",
        "signedRequest": "itissignedRequest",
        "userID": "mp172"
    }
}

JSONLint show following error ,

enter image description here

Runcorn
  • 5,144
  • 5
  • 34
  • 52
0

"Proper" json data has both the property name and string values in double quotes. Browser parsers are very lenient though and the reason I think yours is failing is because it isn't a valid string. When you open the string with ', the string ends on the next ' it finds, so it should be choking when it tries to make sense of connected', after finding the string '{ status : '. If you wrapped your JSON in single double quotes (since it uses single quotes for values) that would probably work, but comphilip is right.

Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113
0

If you intended it to be a string to start with, make sure it is in one line. Otherwise, use (+) to append the string and then use JSON.parse to parse it to an object.

  var exam = '{"status" : "connected","authResponse": {"accessToken": "it is accessToken","expiresIn":"2014-06-19","signedRequest":"it is signedRequest","userID":"mp172"}}'


  var obj = JSON.parse(exam);

Chrome developer tool

Farax
  • 1,447
  • 3
  • 20
  • 37