parsing json using xampp

JavaScript related questions should go here.
Post Reply
jonathon
Posts: 50
Joined: Fri May 06, 2011 5:09 pm

parsing json using xampp

Post by jonathon »

Hi

Does anybody know if xampp comes with a json library for parsing json? I've had a look into the xampp set up and theres a json folder in the zend folder. But having not used zend ever I don't really know if it's the right thing and having also normally worked with XML is AJAX i'm not too familiar with that set up either.

I gather all I need to do is called the json library so I can use parseJSON() rather than using eval.

Would anyone be able to give me some help on the matter if you've done this before?

(I have obviosuly looked around on the net too)

Thanks

Jonathon :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: parsing json using xampp

Post by jacek »

You can just use eval, its fine to do that if the JSON comes from the php json_encode() function.
var data = eval('(' + json_string + ')');
Image
jonathon
Posts: 50
Joined: Fri May 06, 2011 5:09 pm

Re: parsing json using xampp

Post by jonathon »

Hi Jacek,

It does indeed:
$data = array();
		while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
			$data[] = array('name' => $row[0], 'department' => $row[2], 'email' => $row[1]);
		} // End of while loop
		echo json_encode($data) . "\n";
and I was using :
var data = eval('(' + ajax.responseText + ')'); 
I havent done a massive amount of ajax you see, but have heard that eval can be unsafe to use. So thought I'd give parseJSON() a blast as a plan b. But if you say it's all good, then it's all good for me

Thank you ;)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: parsing json using xampp

Post by jacek »

It can be unsafe if you do not know the source of the JSON string, but as you do, and json_encode() apples all the necessary escaping, you are fine to use eval :)
Image
jonathon
Posts: 50
Joined: Fri May 06, 2011 5:09 pm

Re: parsing json using xampp

Post by jonathon »

Well this is what I kind of believed, but I wasn't sure of an example when you wouldn't know the source of the json string?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: parsing json using xampp

Post by jacek »

jonathon wrote:I wasn't sure of an example when you wouldn't know the source of the json string?
Me neither really. Some sites provide an API or something that outputs JSON so I guess they would count. but you can't make an AJAX request to an external domain as far as I know.

It may just mean JSON that has not been encoded very well ;)
Image
jonathon
Posts: 50
Joined: Fri May 06, 2011 5:09 pm

Re: parsing json using xampp

Post by jonathon »

Well, I'm pleased that I can just essentially use eval...... But wtf, I just thought it was me not being particulary experienced in AJAX - oh well. Thanks Jacek (As usual) ;)
Post Reply