简体   繁体   中英

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"
}]

strJSON更改为response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM