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


Groups > comp.lang.javascript > #26475

Re: Help with JSON.parse

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Help with JSON.parse
Date Wed, 07 Jan 2015 01:44:05 +0000
Organization A noiseless patient Spider
Lines 50
Message-ID <878uhfnz4q.fsf@bsb.me.uk> (permalink)
References <D0D1D728.F71C%bint@ign.com>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="19104"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mG5TRYTk+XTheTsz8qLapDFYKwA55gG0="
Cancel-Lock sha1:v/It4UySHSmRp6AVBKwGcyj7Yjc= sha1:Lo9dVqoGw6ap6MBiiVQbPujIGgE=
X-BSB-Auth 1.c1b1395b5bb35101bd5d.20150107014405GMT.878uhfnz4q.fsf@bsb.me.uk
Xref csiph.com comp.lang.javascript:26475

Show key headers only | View raw


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.

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


Thread

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

csiph-web