Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18064 > unrolled thread

Handling numbers via JSON.parse Q's?

Started byBobMCT <r.mariotti@fdcx.net>
First post2013-01-10 21:37 -0500
Last post2013-01-11 21:02 -0500
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Handling numbers via JSON.parse Q's? BobMCT <r.mariotti@fdcx.net> - 2013-01-10 21:37 -0500
    Re: Handling numbers via JSON.parse Q's? Stefan Weiss <krewecherl@gmail.com> - 2013-01-11 03:45 +0100
    Re: Handling numbers via JSON.parse Q's? Denis McMahon <denismfmcmahon@gmail.com> - 2013-01-11 08:39 +0000
      Re: Handling numbers via JSON.parse Q's? Denis McMahon <denismfmcmahon@gmail.com> - 2013-01-11 09:22 +0000
        Re: Handling numbers via JSON.parse Q's? BobMCT <r.mariotti@fdcx.net> - 2013-01-11 21:02 -0500

#18064 — Handling numbers via JSON.parse Q's?

FromBobMCT <r.mariotti@fdcx.net>
Date2013-01-10 21:37 -0500
SubjectHandling numbers via JSON.parse Q's?
Message-ID<6ftue85iefl7i6g4vfo0jnvjnl1nter0fs@4ax.com>
I've recently altered some of my js functions to extract ajax data
from eval to JSON.parse and unfortunately I've lost my numerics that
come down.

My json string looks like this:
{"_stat":null,"_desc":"Powerball","_url":"www.ctlottery.org","_mdup":"N","_bdup":"N","_maxe":"5","_minn":"1","_maxn":"59","_bmaxe":"1","_bminn":"1","_bmaxn":"35"}

and my instruction to extract the values into an associative array is:
Ginfo = JSON.parse(xmlhttp.responseText);

However, when I try to extract a numeric value using the following
instruction:

_maxe = Number(Ginfo['_maxe']);

it appears that the field _maxe is NULL.  Also, when I try to show its
value with an alert('_maxe = ' + _maxe);
the value displayed is NaN (Not a Number.

Using my various js books and wearing out google I'm hoping someone
can point me into the right direction to overcome this issue.

Thank all.

[toc] | [next] | [standalone]


#18066

FromStefan Weiss <krewecherl@gmail.com>
Date2013-01-11 03:45 +0100
Message-ID<kcnuc6$ejp$1@news.albasani.net>
In reply to#18064
On 2013-01-11 03:37, BobMCT wrote:
> My json string looks like this:
> {"_stat":null,"_desc":"Powerball","_url":"www.ctlottery.org","_mdup":"N","_bdup":"N","_maxe":"5","_minn":"1","_maxn":"59","_bmaxe":"1","_bminn":"1","_bmaxn":"35"}
> 
> and my instruction to extract the values into an associative array is:
> Ginfo = JSON.parse(xmlhttp.responseText);
> 
> However, when I try to extract a numeric value using the following
> instruction:
> 
> _maxe = Number(Ginfo['_maxe']);
> 
> it appears that the field _maxe is NULL.  Also, when I try to show its
> value with an alert('_maxe = ' + _maxe);
> the value displayed is NaN (Not a Number.

What you've described should work, so there's something else going on.
Maybe xmlhttp.responseText doesn't contain what you think it does?

- stefan

[toc] | [prev] | [next] | [standalone]


#18067

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-01-11 08:39 +0000
Message-ID<kcoj4n$s5n$1@dont-email.me>
In reply to#18064
On Thu, 10 Jan 2013 21:37:56 -0500, BobMCT wrote:

> I've recently altered some of my js functions to extract ajax data from
> eval to JSON.parse and unfortunately I've lost my numerics that come
> down.
> 
> My json string looks like this:
> 
{"_stat":null,"_desc":"Powerball","_url":"www.ctlottery.org","_mdup":"N",
"_bdup":"N","_maxe":"5","_minn":"1","_maxn":"59","_bmaxe":"1","_bminn":"1",
"_bmaxn":"35"}

How do you know this?

> and my instruction to extract the values into an associative array is:
> Ginfo = JSON.parse(xmlhttp.responseText);

Do you check what gets put into Ginfo?

See <url:http://stackoverflow.com/questions/684672/loop-through-
javascript-object> for a discussion of displaying an arbitrary javascript 
object such as Ginfo.

Or:

// wrap string s to lines of length n
function wrap( s, n ) {
s = String( s );
if ( s.length < n ) return s;
var r = "";
while ( s.length > n ) {
r = r + s.substr( 0, n ) + "\n";
s = s.substr( n );
}
r = r + s + "\n";
return r;
}

alert( wrap( "Ginfo:" + JSON.stringify( Ginfo ), 50 ) );

and check that this is the JSON string that you think Ginfo was created 
from.

> However, when I try to extract a numeric value using the following
> instruction:
> 
> _maxe = Number(Ginfo['_maxe']);
> 
> it appears that the field _maxe is NULL.  Also, when I try to show its
> value with an alert('_maxe = ' + _maxe);
> the value displayed is NaN (Not a Number.

What do you get with:

alert ( "response:" + xmlhttp.responseText );

alert ( "Ginfo['_maxe']:" + Ginfo['_maxe'] );

Note - I've been up all night, I'm tired, and I didn't check any of this 
code would actually work!

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#18068

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-01-11 09:22 +0000
Message-ID<kcoljp$s5n$2@dont-email.me>
In reply to#18067
On Fri, 11 Jan 2013 08:39:51 +0000, Denis McMahon wrote:

> On Thu, 10 Jan 2013 21:37:56 -0500, BobMCT wrote:
> 
>> I've recently altered some of my js functions to extract ajax data from
>> eval to JSON.parse and unfortunately I've lost my numerics that come
>> down.
>> 
>> My json string looks like this:
>> 
> 
{"_stat":null,"_desc":"Powerball","_url":"www.ctlottery.org","_mdup":"N",
> 
"_bdup":"N","_maxe":"5","_minn":"1","_maxn":"59","_bmaxe":"1","_bminn":"1",
> "_bmaxn":"35"}
> 
> How do you know this?
> 
>> and my instruction to extract the values into an associative array is:
>> Ginfo = JSON.parse(xmlhttp.responseText);
> 
> Do you check what gets put into Ginfo?
> 
> See <url:http://stackoverflow.com/questions/684672/loop-through-
> javascript-object> for a discussion of displaying an arbitrary
> javascript object such as Ginfo.
> 
> Or:
> 
> // wrap string s to lines of length n function wrap( s, n ) {
> s = String( s );
> if ( s.length < n ) return s;
> var r = "";
> while ( s.length > n ) {
> r = r + s.substr( 0, n ) + "\n";
> s = s.substr( n );
> }
> r = r + s + "\n";
> return r;
> }
> 
> alert( wrap( "Ginfo:" + JSON.stringify( Ginfo ), 50 ) );
> 
> and check that this is the JSON string that you think Ginfo was created
> from.
> 
>> However, when I try to extract a numeric value using the following
>> instruction:
>> 
>> _maxe = Number(Ginfo['_maxe']);
>> 
>> it appears that the field _maxe is NULL.  Also, when I try to show its
>> value with an alert('_maxe = ' + _maxe);
>> the value displayed is NaN (Not a Number.
> 
> What do you get with:
> 
> alert ( "response:" + xmlhttp.responseText );
> 
> alert ( "Ginfo['_maxe']:" + Ginfo['_maxe'] );
> 
> Note - I've been up all night, I'm tired, and I didn't check any of this
> code would actually work!

Did some testing, this shows the results:

http://www.sined.co.uk/tmp/jstest.htm

Is it possible that when you create the string in the server side app 
that you're not generating the string youvthink you are?

If you're manually generating the string instead of using a JSON.stringify 
style function on a server side data object such as a php associative 
array, for example, you may need to escape all the double quotes inside 
the JSON string.

eg

$r = "{\"$att1\":\"$val1\",...,\"$attN\":\"$valN\"}";
header('Content-type: text/plain');
echo $r;

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#18085

FromBobMCT <r.mariotti@fdcx.net>
Date2013-01-11 21:02 -0500
Message-ID<irg1f81akbci926flcekb3vbm8nt4t3v7g@4ax.com>
In reply to#18068
On Fri, 11 Jan 2013 09:22:01 +0000 (UTC), Denis McMahon
<denismfmcmahon@gmail.com> wrote:

>
>Did some testing, this shows the results:
>
>http://www.sined.co.uk/tmp/jstest.htm
>
>Is it possible that when you create the string in the server side app 
>that you're not generating the string youvthink you are?
>
>If you're manually generating the string instead of using a JSON.stringify 
>style function on a server side data object such as a php associative 
>array, for example, you may need to escape all the double quotes inside 
>the JSON string.
>
>eg
>
>$r = "{\"$att1\":\"$val1\",...,\"$attN\":\"$valN\"}";
>header('Content-type: text/plain');
>echo $r;


Thanks for these several replies.  After I posted this I reverted back
from JSON.parse to the old EVAL on the same returned string (whch was
verified on the client using Firebug js debugging).  
By using eval everything went back to working.  So I would think that
Dennis's suggestion about stringifying for JSON would do the trick.
Thanks for the suggestion and I've give it a go.

Cheers

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web