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


Groups > comp.lang.javascript > #18068

Re: Handling numbers via JSON.parse Q's?

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Handling numbers via JSON.parse Q's?
Date 2013-01-11 09:22 +0000
Organization A noiseless patient Spider
Message-ID <kcoljp$s5n$2@dont-email.me> (permalink)
References <6ftue85iefl7i6g4vfo0jnvjnl1nter0fs@4ax.com> <kcoj4n$s5n$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web