var jsonData = JSON.parse(jsonString);
Now, we previously covered maps. Maps have keys and values: the keys unlock a particular value. For example, take this JavaScript:
var map = { "key": "value", "key2": "value2" };
We define here a key-value object, and we can access the two values with the two keys:
map["key2"]; // == value2
This is where the JSON format comes in. We can convert the content of the above map variable into a string representation with this code:
var mapStringified = JSON.stringify(map);
which gives us the following output:
{"key":"value","key2":"value2"}
This string looks an awful lot like the JavaScript code we wrote earlier.
And, likewise we can convert the JSON string back into a JavaScript data model again, with:
var mapAgain = JSON.parse(mapStringified);
Now, I did warn you earlier that values can be lists and maps itself again, so consider this JSON example from Wikipedia:
{
"id": 1,
"name": "Foo",
"price": 123,
"tags": [ "Bar", "Eek" ],
"stock": {
"warehouse": 300,
"retail": 20
}
}
Here we see that the value behind the stock key is another map, and the value behind the tags key is a list. This creates a quite flexible serialization format, which is happily used by Open PHACTS. (And for the semantic web readers, yes, we can make JSON more semantic. The Open PHACTS LDA supports a "rdfjson" format.)
No comments:
Post a Comment