JSON.Stringify: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(Created page with "JSON.STRINGIFY (''object''[, ''replacer'']) '''Description''' JSON.STRINGIFY converts an NS Basic object to a string. The data is saved in JSON format, which is a widely use...")
 
No edit summary
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
JSON.STRINGIFY (''object''[, ''replacer''])
JSON.stringify (''object''[, ''replacer''])


'''Description'''
== Description ==


JSON.STRINGIFY converts an NS Basic 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.
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.


'''Example'''
A handy tool to look at JSON strings is located at http://www.jsoneditoronline.org.


<pre>
== Example ==
REM JSON.STRINGIFY Example
 
<tabber>
JavaScript=
<syntaxhighlight lang="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;
  }
}
</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
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.stringify(myObject, replacer)
s=JSON.stringify(myObject, replacer)
Line 20: Line 40:
   End If
   End If
End Function
End Function
</pre>
</syntaxhighlight>
</tabber>


'''Output'''
== Output ==


<pre>
<pre>
Line 28: Line 49:
</pre>
</pre>


'''Related Items'''
== Related Items ==
 
[[JSON.Parse|JSON.Parse]]
 
[[Category:Language Reference]]


[[json.parse|JSON.PARSE]]
[[Category:Miscellaneous]]

Latest revision as of 22:43, 24 July 2019

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

// 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;
  }
}

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

Output

MyObject as a string: {"a":"12355","b":4,"c":[1,2,3,4]}

Related Items

JSON.Parse