Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   How to use JSON.parse method? (http://www.velocityreviews.com/forums/t939952-how-to-use-json-parse-method.html)

DL 11-26-2009 08:17 PM

How to use JSON.parse method?
 

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.






Richard Cornford 11-27-2009 02:33 PM

Re: How to use JSON.parse method?
 
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 11-28-2009 12:23 AM

Re: How to use JSON.parse method?
 
On Nov 27, 9:33*am, Richard Cornford <Rich...@litotes.demon.co.uk>
wrote:
> 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.


Thank you both, I've taken another route.


All times are GMT. The time now is 03:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.