How to use JSON.parse method?

D

DL

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.
 
R

Richard Cornford

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.
 
D

DL

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


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


  ^^
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.


What does the documentation say?


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.

Thank you both, I've taken another route.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top