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


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

Help with JSON.parse

Started byBint <bint@ign.com>
First post2015-01-06 18:17 -0600
Last post2015-01-07 11:10 +0100
Articles 4 — 4 participants

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


Contents

  Help with  JSON.parse Bint <bint@ign.com> - 2015-01-06 18:17 -0600
    Re: Help with  JSON.parse Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-07 01:44 +0000
    Re: Help with  JSON.parse Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-07 03:47 +0000
      Re: Help with  JSON.parse Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-07 11:10 +0100

#26473 — Help with JSON.parse

FromBint <bint@ign.com>
Date2015-01-06 18:17 -0600
SubjectHelp with JSON.parse
Message-ID<D0D1D728.F71C%bint@ign.com>
Hello,

  I don't have a whole lot of experience with JSON.  I am trying to return a
two-dimensional array from PHP to Javascript.  The array contains some
strings, however, which seem to screw up the JSON parsing.  The strings
contain parenthesis which I guess JSON uses.

But shouldn't the JSON_encode call take care of that?  I'm confusing.

So I have an array.  Here are two entries, where each entry has three
sub-strings.

"(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is string 1."
"(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 2"

I json_encode that from PHP. But when I use JSON.Parse back in Javascript, I
get an error: SyntaxError: JSON Parse error: Unexpected token '('

Anyone know what I need to do to fix it?  Do I need to further encode those
strings with parenthesis before sticking them in the array for JSON to
encode?  

Thanks!


[toc] | [next] | [standalone]


#26475

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-01-07 01:44 +0000
Message-ID<878uhfnz4q.fsf@bsb.me.uk>
In reply to#26473
Bint <bint@ign.com> writes:

>   I don't have a whole lot of experience with JSON.  I am trying to return a
> two-dimensional array from PHP to Javascript.  The array contains some
> strings, however, which seem to screw up the JSON parsing.  The strings
> contain parenthesis which I guess JSON uses.
>
> But shouldn't the JSON_encode call take care of that?  I'm confusing.
>
> So I have an array.  Here are two entries, where each entry has three
> sub-strings.
>
> "(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is string 1."
> "(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 2"
>
> I json_encode that from PHP. But when I use JSON.Parse back in Javascript, I
> get an error: SyntaxError: JSON Parse error: Unexpected token '('

The problem is somewhere in the code, and you don't show any code!  This
PHP:

  $a = [
       ["(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is string 1."],
       ["(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 2"]
  ];
  echo json_encode($a);

correctly outputs (sorry about the long line):

[["(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is string 1."],["(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 2"]]

And this is correctly parsed by JSON.parse:

JSON.parse('[["(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is string 1."],["(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 2"]]')

produces:

[ [ '(Jan 7, 2015 0:42AM)',
    '2015-01-06 15:43:00',
    'this is string 1.' ],
  [ '(Jan 7, 2015 0:42AM)',
    '2015-01-06 15:42:26',
    'this is string 2' ] ]

I suspect some problem to do with quotes in the way you get the string
from PHP to JavaScript.

<snip>
-- 
Ben.

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


#26479

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-01-07 03:47 +0000
Message-ID<m8ia9a$ofv$1@dont-email.me>
In reply to#26473
On Tue, 06 Jan 2015 18:17:12 -0600, Bint wrote:

> So I have an array.  Here are two entries, where each entry has three
> sub-strings.

> "(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is string 1."
> "(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 2"

> I json_encode that from PHP. But when I use JSON.Parse back in
> Javascript, I get an error: SyntaxError: JSON Parse error: Unexpected
> token '('

What language is this data in? It doesn't look like an array in PHP or 
JaVaScRiPt(TM)(C)[1] or a JSON string to me, it just looks like two lists 
of quoted strings written out in a newsreader.

As a json string being an array of two three element arrays, it would 
look like this:

'[["(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is string 1."],
["(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 2"]]'

Parsing this in Python:

>>> json.loads(r'[["(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is 
string 1."],["(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 
2"]]')

[[u'(Jan 7, 2015 0:42AM)', u'2015-01-06 15:43:00', u'this is string 1.'], 
[u'(Jan 7, 2015 0:42AM)', u'2015-01-06 15:42:26', u'this is string 2']]

Parsing it in firefox web console:

JSON.parse('[["(Jan 7, 2015 0:42AM)","2015-01-06 15:43:00","this is 
string 1."],["(Jan 7, 2015 0:42AM)","2015-01-06 15:42:26","this is string 
2"]]');

Array [ Array[3], Array[3] ]

Parsing it in PHP:

<?php

$thing = json_decode('[["(Jan 7, 2015 0:42AM)","2015-01-06 
15:43:00","this is string 1."],["(Jan 7, 2015 0:42AM)","2015-01-06 
15:42:26","this is string 2"]]');

print_r($thing);

Array
(
    [0] => Array
        (
            [0] => (Jan 7, 2015 0:42AM)
            [1] => 2015-01-06 15:43:00
            [2] => this is string 1.
        )

    [1] => Array
        (
            [0] => (Jan 7, 2015 0:42AM)
            [1] => 2015-01-06 15:42:26
            [2] => this is string 2
        )
)

The PHP and JaVaScRiPt(TM)(C)[1] json encoders work fine, the problem is 
invariably in the data passed to them not being formatted correctly, so 
unless you show us the real data and code we will not be able to help.

From the JaVaScRiPt(TM)(C)[1] newsgroup viewpoint, we are really only 
interested in JaVaScRiPt(TM)(C)[1] JSON parser side of the problem, so 
please endeavour to capture and display the actual string that is being 
passed to the encoder. If your PHP is generating bad JSON, that's a PHP 
issue, not a JaVaScRiPt(TM)(C)[1] issue.

Methods of displaying the string include outputting it on the page or in 
the console log prior to or instead of passing it to the JaVaScRiPt(TM)(C)
[1] JSON parser. Note that we really do need to see the actual string, 
and not some generalised version of what [you think] it looks like if we 
are to help you.

[1] tpelbait! tpelbait will continue until tpel STFUs about such mundane 
trivialities as the capitalization of the word JaVaScRiPt(TM)(C).

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#26480

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-07 11:10 +0100
Message-ID<1788147.bP5RWj5sB5@PointedEars.de>
In reply to#26479
Denis McMahon wrote:

> What language is this data in? It doesn't look like an array in PHP or
> JaVaScRiPt(TM)(C)[1] or a JSON string to me, it just looks like two lists
> of quoted strings written out in a newsreader.
> […]
> [1] tpelbait! tpelbait will continue until tpel STFUs about such mundane
> trivialities as the capitalization of the word JaVaScRiPt(TM)(C).

You still have not understood.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


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


csiph-web