Page 1 of 1

JSON.stringify Not Correct

Posted: Mon Jan 18, 2016 10:25 am
by Grandong
Hi...
i try to post json result to php page, with JSON.stringify...
json code like this:

Code: Select all

[
          {
            "airline_code": "QG",
            "fno": "QG 815",
            "from": "Jakarta (Soekarno Hatta)",
            "to": "Surabaya",
            "std": "4:10",
            "sta": "5:35",
            "date": "21-01-2016",
            "stop_over": "0",
            "info": {
              "basic": "417000",
              "seat": null,
              "class": null,
              "order_key": "0~P~~P~RGFR~~1~X|QG~ 815~ ~~CGK~01/21/2016 04:10~SUB~01/21/2016 05:35~"
            }
          }
        ]
but, this don't work... why ?

Re: JSON.stringify Not Correct

Posted: Mon Jan 18, 2016 10:33 am
by Celauran
Grandong wrote:but, this don't work...
You'll need to be a lot more specific. You've posted an array containing a single object. JSON.stringify converts that to a string representation. What were you trying to do? How were you doing it? What does "it doesn't work" mean in this case? What was the expected result? What happened instead?

Re: JSON.stringify Not Correct

Posted: Mon Jan 18, 2016 10:45 am
by Grandong
Celauran wrote:You'll need to be a lot more specific. You've posted an array containing a single object. JSON.stringify converts that to a string representation. What were you trying to do? How were you doing it? What does "it doesn't work" mean in this case? What was the expected result? What happened instead?
This Wrong Result JSON.stringify:
Image

and this Correct Result :
Image

Re: JSON.stringify Not Correct

Posted: Mon Jan 18, 2016 12:37 pm
by Christopher

Code: Select all

[
          {
            "airline_code": "QG",
            "fno": "QG 815",
            "from": "Jakarta (Soekarno Hatta)",
            "to": "Surabaya",
            "std": "4:10",
            "sta": "5:35",
            "date": "21-01-2016",
            "stop_over": "0",
            "info": {
              "basic": "417000",
              "seat": null,
              "class": null,
              "order_key": "0~P~~P~RGFR~~1~X|QG~ 815~ ~~CGK~01/21/2016 04:10~SUB~01/21/2016 05:35~"
            }
          }
        ]
This is the JSON.stringify for the above JSON that you say is wrong:
Image

and this is the JSON.stringify for some other JSON (not shown) that you say is right:
Image
So where is the JSON for the second one that you say is right?

Re: JSON.stringify Not Correct

Posted: Mon Jan 18, 2016 1:11 pm
by Grandong
Christopher wrote:So where is the JSON for the second one that you say is right?
This Correct json result:

Code: Select all

[{
	"airline_code": "SJ",
	"fno": "SJ224",
	"from": "CGK",
	"to": "SUB",
	"std": "07:10",
	"sta": "09:30",
	"date": "21-01-2016",
	"stop_over": "1",
	"info": {
		"basic": "925000",
		"seat": "2",
		"class": "M",
		"order_key": "5457236:M:S:CGK:SUB:U2s5VlVrNUZXUT09"
	}
}]

Re: JSON.stringify Not Correct

Posted: Mon Jan 18, 2016 2:13 pm
by requinix
The structure is correct (though the data is different). The only thing I see is the inconsistent highlighting within the HTML in that one screenshot...

Re: JSON.stringify Not Correct

Posted: Mon Jan 18, 2016 2:59 pm
by Christopher
Grandong wrote:This Correct json result:
You didn't need to post that ... I can see the stringified version! ;) I was just pointing out that you get out what you put in.

You do need to use urlencode(), htmlspecialchars(), or something to put JSON as the value of a HTML tag. Specifically you need to encode the double quotes. Which encoding will depend on what the receiving page is expecting.