[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Help with JSON.parse

Bint

1/7/2015 12:17:00 AM

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!



3 Answers

Ben Bacarisse

1/7/2015 1:44:00 AM

0

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.

Denis McMahon

1/7/2015 3:48:00 AM

0

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

Thomas 'PointedEars' Lahn

1/7/2015 10:10:00 AM

0

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.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.