JSON.stringify Not Correct

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

JSON.stringify Not Correct

Post 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 ?
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: JSON.stringify Not Correct

Post 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?
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: JSON.stringify Not Correct

Post 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
Newbie The Passion for Learning
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: JSON.stringify Not Correct

Post 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?
(#10850)
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: JSON.stringify Not Correct

Post 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"
	}
}]
Newbie The Passion for Learning
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: JSON.stringify Not Correct

Post 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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: JSON.stringify Not Correct

Post 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.
(#10850)
Post Reply