programmers_39
programmers_39

Reputation: 89

how can i parse JSON using Javascript

I am facing some problem in parsing a json response. The problem is to extract the success message from the response below:

{
  "success": true,
  "reports": [
    { "uid": 10, "report_count": 10, "levels":1, "max_download_capacity": 10 },
    { "uid": 11, "report_count":20, "levels": 2, "max_download_capacity": 30 }
  ]
}

Upvotes: 2

Views: 80

Answers (1)

Rick Hitchcock
Rick Hitchcock

Reputation: 35680

Use JSON.parse(JSONString).success, where JSONString is the JSON response to be parsed:

var s ='{"success":true,"reports":[{"uid":10,"report_count":10,"levels":1,"max_download_capacity":10},{"uid":11,"report_count":20,"levels":2,"max_download_capacity":30}]}'

console.log(JSON.parse(s).success);

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Upvotes: 2

Related Questions