JSON.Parse: Difference between revisions
Jump to navigation
Jump to search
Add javascript snippet |
|||
Line 7: | Line 7: | ||
A handy tool to look at JSON strings is located at http://www.jsoneditoronline.org. | A handy tool to look at JSON strings is located at http://www.jsoneditoronline.org. | ||
== Example == | == Example (Basic) == | ||
<pre> | <pre> | ||
Line 21: | Line 21: | ||
End If | End If | ||
End Function | End Function | ||
</pre> | |||
== Example (JavaScript) == | |||
<pre> | |||
// JSON.Parse Example | |||
myObject=JSON.Parse(s, reviver); | |||
NSB.Print("myObject.a:" + myObject.a + " myObject.b:" + myObject.b + "<br>"); | |||
reviver = function(key, value) { | |||
if(key=="a" && TypeName(Value)== "Integer") { | |||
return value.toString(); //If value is a number, we want to change it to string. | |||
} else { | |||
return value; | |||
} | |||
} | |||
</pre> | </pre> | ||
Revision as of 01:10, 28 May 2013
JSON.Parse (string[, reviver])
Description
JSON.Parse converts a string created by JSON.Stringify into an object. It is useful for interchanging data with other systems and for saving objects to localStorage and databases. string is the string to be converted. reviver is an optional function name that can be used to do some processing of the data while being parsed.
A handy tool to look at JSON strings is located at http://www.jsoneditoronline.org.
Example (Basic)
Rem JSON.Parse Example myObject=JSON.Parse(s, reviver) Print "myObject.a:" & myObject.a & " myObject.b:" & myObject.b Function reviver(key, value) If key="a" and TypeName(Value)= "Integer" Then reviver=CStr(value) 'If value is a number, we want to change it to string. Else reviver=value End If End Function
Example (JavaScript)
// JSON.Parse Example myObject=JSON.Parse(s, reviver); NSB.Print("myObject.a:" + myObject.a + " myObject.b:" + myObject.b + "<br>"); reviver = function(key, value) { if(key=="a" && TypeName(Value)== "Integer") { return value.toString(); //If value is a number, we want to change it to string. } else { return value; } }
Output
myObject.a:12355 myObject.b:4