Denis McMahon
1/7/2015 3:48:00 AM
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