Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to use JSON.parse method?

Reply
Thread Tools

How to use JSON.parse method?

 
 
DL
Guest
Posts: n/a
 
      11-26-2009

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.





 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      11-27-2009
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.
 
Reply With Quote
 
 
 
 
DL
Guest
Posts: n/a
 
      11-28-2009
On Nov 27, 9:33*am, Richard Cornford <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
Can I use XPath or something to a remote Mac or Linux box and just query an xml file, not using web services and use encyrption? jake ASP .Net 0 07-06-2004 02:16 PM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM



Advertisments