JSON.Stringify: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
JwellsNB (talk | contribs)
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 22: Line 22:
   End If
   End If
End Function
End Function
</pre>
== Example (JavaScript) ==
<pre>
// 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;
  }
}
</pre>
</pre>



Revision as of 01:17, 28 May 2013

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]}

Related Items

JSON.Parse