JSON.Stringify: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 1: | Line 1: | ||
JSON. | JSON.stringify (''object''[, ''replacer'']) | ||
== Description == | == Description == | ||
JSON. | JSON.stringify converts an object to a string. The data is saved in JSON format, which is a widely used standard for exchanging data. Many platforms and programming languages provide Json unpacking routines. ''object'' is the object to be converted. ''replacer'' is an optional function to reformat the values. | ||
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. | ||
Line 10: | Line 10: | ||
<pre> | <pre> | ||
Rem JSON. | Rem JSON.stringify Example | ||
myObject={a:"12355", b:2, c:[1,2,3,4]} | myObject={a:"12355", b:2, c:[1,2,3,4]} | ||
s=JSON. | s=JSON.stringify(myObject, replacer) | ||
Print "MyObject as a string: " & s | Print "MyObject as a string: " & s | ||
Revision as of 17:32, 2 May 2016
JSON.stringify (object[, replacer])
Description
JSON.stringify converts an object to a string. The data is saved in JSON format, which is a widely used standard for exchanging data. Many platforms and programming languages provide Json unpacking routines. object is the object to be converted. replacer is an optional function to reformat the values.
A handy tool to look at JSON strings is located at http://www.jsoneditoronline.org.
Example (Basic)
Rem JSON.stringify Example myObject={a:"12355", b:2, c:[1,2,3,4]} s=JSON.stringify(myObject, replacer) Print "MyObject as a string: " & s Function replacer(key, value) If key="b" Then replacer=value*2 'Just for fun, we'll save double the value of b. Else replacer=value End If End Function
Example (JavaScript)
// JSON.stringify Example myObject={a:"12355", b:2, c:[1,2,3,4]} s=JSON.stringify(myObject, replacer); NSB.Print("MyObject as a string: " + s)); function replacer(key, value) { if(key=="b") { return value*2; //Just for fun, we'll save double the value of b. } else { return value; } }
Output
MyObject as a string: {"a":"12355","b":4,"c":[1,2,3,4]}