Vijay
Vijay

Reputation: 5433

how to convert the string into json object?

I use the ajax which sends back a string..

I want to convert the responsetext into a json object to process.

I tried eval and also , but doesn't works...

Wht to do?

My code is

function handleResponse() {
  if (httpa.readyState == 4) {
    var response = httpa.responseText;
    if (response != 'empty') {
      alert(response);
      var foo = eval('(' + strJSON + ')');
      alert(foo);
    }
  }
}

// response alerts

[{
  "id": "1",
  "name": "Pepsodent 100g",
  "selling_price": "28.75"
}, {
  "id": "2",
  "name": "Pepsodent 40g",
  "selling_price": "18.90"
}, {
  "id": "3",
  "name": "Pepsodent brush",
  "selling_price": "19.50"
}]

Upvotes: 1

Views: 3442

Answers (2)

Eugene Ramirez
Eugene Ramirez

Reputation: 3123

Using https://github.com/douglascrockford/JSON-js/blob/master/json2.js

you can do

JSON.parse(response, reviver)

http://www.json.org/js.html

Upvotes: 2

SLaks
SLaks

Reputation: 887215

Change strJSON to response.

Upvotes: 1

Related Questions