-2

I'm stringyfing an object like:

"{'foo': 'bar','task':[{'task1':'task1'}]}"

How can I turn the string back to an object?

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • `java` and `jsonp` have basically nothing to do with each other. Did you mean `javascript`? – T.J. Crowder Apr 02 '13 at 07:45
  • parse the `JSON`. How are you converting your object to `JSON`? – Rahul Apr 02 '13 at 07:46
  • 1
    [`JSON.parse`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse) – Christoph Apr 02 '13 at 07:49
  • You are unclear with your question. Where do you want to parse your JSON. Server or clientside? Which language do you use for processing? – Christoph Apr 02 '13 at 08:15
  • possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – Christoph Apr 02 '13 at 08:47

1 Answers1

2

Try the following solution to parse the stringified JSONObject.

// Stringified JSON Object
var stringifiedJSONObject = '{'foo': 'bar','task':[{'task1':'task1'}]}';

// Parsing string object to json
var jsonObject = JSON.parse(stringifiedJSONObject);

// Get the inner array. The below object is a JSON Array of Objects
var innerArray = jsonObject.task;

// displays the value of task1
alert(innerArray[0].task1);
Ankur Jain
  • 1,386
  • 3
  • 17
  • 27
  • `var innerJsonArray = jsonObject.task;` does no parsing at all. Also the object is no JSON Array, it's a plain javascript array, which (how surprising!) happens to have the same notation like a *J*ava*S*cript*O*bject*N*otation Array! – Christoph Apr 02 '13 at 07:55
  • @Christoph Well I tried to give a generic solution, and JSON array is also ultimately a JavaScript array. And take a look he is also wants to parse another JsonObject array inside the jsonobject which contains a JSONArray. – Ankur Jain Apr 02 '13 at 08:00
  • You are confusing some things here. JSON is a data transfer format, which uses the Javascript Literal Notation. It's completely independent and has nothing to do with javascript. You can use JSON with every language you like. Once you parse the JSON-String with javascript you have a Javascript Object. The parsing step is only done once. After that you can access the object and all it properties like you do normally in javascript. Also with a highly nested JSON, there is no further parsing needed, once it is parsed, you have an equivalent javascript object. – Christoph Apr 02 '13 at 08:11
  • In your answer, `stringifiedJSONObject` is ***not*** JSON. It's a JavaScript object. `JSON.parse(stringifiedJSONObject)` will fail here. – gen_Eric Apr 03 '13 at 13:58