Page 1 of 1
parsing json using xampp
Posted: Wed May 18, 2011 10:33 am
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
Re: parsing json using xampp
Posted: Wed May 18, 2011 1:46 pm
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 + ')');
Re: parsing json using xampp
Posted: Wed May 18, 2011 6:40 pm
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
Re: parsing json using xampp
Posted: Wed May 18, 2011 7:11 pm
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
Re: parsing json using xampp
Posted: Wed May 18, 2011 7:28 pm
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?
Re: parsing json using xampp
Posted: Wed May 18, 2011 7:34 pm
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
Re: parsing json using xampp
Posted: Wed May 18, 2011 7:38 pm
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)