Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use JSON.parse method?

1 view
Skip to first unread message

DL

unread,
Nov 26, 2009, 3:17:04 PM11/26/09
to

MSDN has an interesting example. However, what I'd like to do is to
evaluate if the data set is JSON compliant. For instance,
var jdata = "name: don; sex: m";
var notJdata = "<nuts... sjkdks klsd, name, sie jsjd, sex,
ksdksk.....">

Attempt to use it like the following produces nothing
If (JSON.parse(jdata))
{
alert ('jason data')
};

How am I supposed to use it? Or if not using JSON.parse, what is
already available within the javascript language?

Thanks.

Hans-Georg Michna

unread,
Nov 27, 2009, 9:00:26 AM11/27/09
to

JSON.parse is not just there to check for JSON compliance. It
actually turns JSON-formatted text into JavaScript objects or
arrays.

For a very simple example, the statement:

var o = JSON.parse('{"x":"Hello","y":"world!"}');

Has the same effect as:

var o = {"x":"Hello","y":"world!"};

But it works only for a limited set of stringified JavaScript
literals.

var s = JSON.stringify(o);

does just the reverse. s now contains the string:

'{"x":"Hello","y":"world!"}'

Read up on JSON: http://json.org/

Hans-Georg

Richard Cornford

unread,
Nov 27, 2009, 9:33:26 AM11/27/09
to
On Nov 26, 8:17 pm, DL wrote:
> MSDN has an interesting example. However, what I'd like
> to do is to evaluate if the data set is JSON compliant.
> For instance,
> var jdata = "name: don; sex: m";

That is not JSON data (it needs a set of brace characters around the
contents).

> var notJdata = "<nuts... sjkdks klsd, name, sie jsjd, sex,
> ksdksk.....">

Even without the carriage return, that is a syntax error, so not
javascript source code at all.

> Attempt to use it like the following produces nothing
> If (JSON.parse(jdata))

^^
An attempt to call an undefined/undeclared function named - If - will
generate a runtime error, and generating an error is not producing
"nothing". But as the - jdata - defined above was not a JSON string
the outcome without the above syntax error and the runtime error here
would probably still qualify as your "nothing".

> {

Styles of code formatting get argued about quite a bit, with very
little of significance to back up any particular style (that is, it is
mostly (somebody's) personal preference). I prefer not putting a
carriage return before the opening brace here (so that it would have
been on the end of the previous line). If that style had been used
here, using - If - in place of - if - would have resulted in a syntax
error instead of a runtime error. This would have been preferable as
you get informed of that when the code is loaded, while a runtime
error only gets triggered in the event that execution encounters this
particular code.

For anyone wondering; it was javascript's automatic semicolon
insertion that prevented this from being a syntax error. The carriage
return before the opening brace gave the automatic semicolon insertion
system an opportunity to inset a semicolon turning the above into the
syntax for a function call followed by a Block statement.

> alert ('jason data')
>
> };
>
> How am I supposed to use it?

What does the documentation say?

> Or if not using JSON.parse, what is
> already available within the javascript language?

Either the code here does not show what you are actually doing, or
explains why it does not 'work', independently of the JSON.parse
behaviour, so suggesting what else may allow you to do whatever it is
you are attempting to do is not practical.

Richard.

DL

unread,
Nov 27, 2009, 7:23:34 PM11/27/09
to
On Nov 27, 9:33 am, Richard Cornford <Rich...@litotes.demon.co.uk>
wrote:

Thank you both, I've taken another route.

0 new messages