Answered by:
JSON and eval

Question
-
In Metro what is the recommended method of converting a JSON string into a JavaScript object? Do we still have to use 'eval' (after verifying that string is valid) or we can use Windows.Data.Json WinRT object? In MSDN I didnt see JavaScript usage of Windows.Data.Json hence the question.
evals() historically in jscript.dll was one of the slowest operations (in terms of CPU cycles). Has that changed in Windows 8, given that many Metro apps will use JavaScript and rely heavily on JSON?
Wednesday, October 19, 2011 12:57 AM
Answers
-
This works well for me:
var myObject = JSON.parse(myJSONstring);
and reverse:
var myJSONstring = JSON.stringify(myObject);
- Marked as answer by Sameer Karkhanis Wednesday, October 19, 2011 7:09 PM
Wednesday, October 19, 2011 3:30 AM -
The SDK samples I've seen use JSON.parse as jrboddie suggests. the JSON.* APIs are supported natively in the Metro style app environment.
Note that the Windows.Data.Json API in WinRT is actually not made available to apps written in JavaScript because JSON.* is already here. You'll find that the WinRT APIs are oriented around strongly-typed languages like C#, VB, and C++, and for which other APIs aren't available.
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, October 19, 2011 12:36 PM
- Marked as answer by Sameer Karkhanis Wednesday, October 19, 2011 7:09 PM
Wednesday, October 19, 2011 4:20 AM
All replies
-
This works well for me:
var myObject = JSON.parse(myJSONstring);
and reverse:
var myJSONstring = JSON.stringify(myObject);
- Marked as answer by Sameer Karkhanis Wednesday, October 19, 2011 7:09 PM
Wednesday, October 19, 2011 3:30 AM -
The SDK samples I've seen use JSON.parse as jrboddie suggests. the JSON.* APIs are supported natively in the Metro style app environment.
Note that the Windows.Data.Json API in WinRT is actually not made available to apps written in JavaScript because JSON.* is already here. You'll find that the WinRT APIs are oriented around strongly-typed languages like C#, VB, and C++, and for which other APIs aren't available.
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, October 19, 2011 12:36 PM
- Marked as answer by Sameer Karkhanis Wednesday, October 19, 2011 7:09 PM
Wednesday, October 19, 2011 4:20 AM -
Thanks jrboddie and KraigWednesday, October 19, 2011 3:16 PM
-
One thing to keep in mind with using JSON.parse/JSON.stringify -- JavaScript Date objects will serialize to a string but they will not revive as Dates. So if you take a simple object that has a date as one of its properties and do a stringify and then a parse you will end up with a string instead of a Date.
To work around this you can pass a custom reviver function to JSON.parse() and there just so happens to be the exact code to do this on the MSDN documentation page for JSON.parse:
http://msdn.microsoft.com/en-us/library/cc836466(VS.85).aspx
Cheers,
-Jeff
Wednesday, October 19, 2011 8:13 PM -
I was unable to find a "parse" method anywhere in Windows.Data.Json, either in JsonValue or in JsonObject. I was able to construct a JsonObject from a string, but that pre-supposes I know I've got an object in the JSON string. Where do I find the parse method? Thanks.Sunday, February 19, 2012 10:39 AM