|
|
(4 intermediate revisions by 3 users not shown) |
Line 1: |
Line 1: |
| JSON.Parse (''string''[, ''reviver'']) | | JSON.parse (''string''[, ''reviver'']) |
|
| |
|
| == Description == | | == 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. | | 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. | | A handy tool to look at JSON strings is located at http://www.jsoneditoronline.org. |
|
| |
|
| == Example (Basic) == | | == Example == |
|
| |
|
| <pre> | | <tabber> |
| Rem JSON.Parse Example
| | JavaScript= |
| myObject=JSON.Parse(s, reviver)
| | <syntaxhighlight lang="JavaScript"> |
| 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
| |
| </pre> | |
| | |
| == Example (JavaScript) == | |
| <pre>
| |
| // JSON.parse Example | | // JSON.parse Example |
| myObject=JSON && JSON.parse(s, reviver) || $.parseJSON(s, reviver); | | myObject=JSON && JSON.parse(s, reviver) || $.parseJSON(s, reviver); |
| NSB.Print("myObject.a:" + myObject.a + " myObject.b:" + myObject.b + "<br>"); | | NSB.Print("myObject.a:" + myObject.a + " myObject.b:" + myObject.b); |
|
| |
|
| reviver = function(key, value) { | | reviver = function(key, value) { |
Line 36: |
Line 23: |
| } | | } |
| } | | } |
| </pre> | | </syntaxhighlight> |
| | |-| |
| | BASIC= |
| | <syntaxhighlight lang="vb.net"> |
| | Rem JSON.parse Example |
| | Dim s = "{""a"": ""this is string a"", ""b"": 99}" |
| | 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 |
| | </syntaxhighlight> |
| | </tabber> |
|
| |
|
| == Output == | | == Output == |
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
// JSON.parse Example
myObject=JSON && JSON.parse(s, reviver) || $.parseJSON(s, reviver);
NSB.Print("myObject.a:" + myObject.a + " myObject.b:" + myObject.b);
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;
}
}
Rem JSON.parse Example
Dim s = "{""a"": ""this is string a"", ""b"": 99}"
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
Output
myObject.a:12355 myObject.b:4
Related Items
JSON.Stringify