0

I have a JavaScript object named page.cat.lastResponseText.

This object has the following string value:

{
    "orderBy":"",
    "orderDesc":false,
    "rows":[
        {"catIndId":"3","indId":"1","catId":"2"},         
        {"catIndId":"4","indId":"4","catId":"2"}
    ],
    "totalResults":2,
    "totalPages":1,
    "pageSize":2,
    "currentPage":1
}

How I can convert this string into a JSON object? And how do I then get the values for object such as cat.rows or cat.totalResults?

1983
  • 5,882
  • 2
  • 27
  • 39
xzegga
  • 3,051
  • 3
  • 25
  • 45
  • 2
    This probably took you much longer than just typing "javascript string to json" in google. – php_nub_qq Aug 02 '15 at 16:36
  • Your question seems quite confused. You say you have a "JavaScript object", but then you say it's a string. I guess what you mean to say is "a string representation of an object". And I doubt if you want to convert it into a "JSON object"; I suppose you mean to say a "JavaScript object". in any case, yes, the way you do it is to parse the JSON string. –  Aug 02 '15 at 19:18
  • The question addresses not only the problem how to parse a JSON (which, ok, is duplicated), but also how to get then a specific value (so an appropriate search in google leads to this question). For the provided in the question example after parsing (`var mJsonObj=JSON.parse(jsonString)`) you can get specific value as follows: `var mVal=mJsonObj.orderDesc` gives `false`, `mJsonObj.totalResults` gives `2` and `mJsonObj.rows[0].catIndId` gives `3`, `mJsonObj.rows[1].catIndId` gives `4`. – Elia12345 Sep 05 '15 at 20:20

1 Answers1

1

Simply use JSON.stringify() and JSON.parse()

http://caniuse.com/#feat=json

Tibor Szasz
  • 3,028
  • 2
  • 18
  • 23