JSON Data Handling

Out put JSON document using PHP:

header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json;charset=utf-8');
 
$array = array();
 
if(strlen($err) == 0 && $errCode == 0){
    while($row = mysql_fetch_assoc($result)){
		$arr = array('name'=>$row['category'] . ' ' . $row['name'], 'value'=>$row['id']);
		array_push($array, $arr);      
    }
}
 
echo json_encode($array);

This example will output JSON object and don’t neet $.parseJSON() to parse it.

 

If javascript still get a “String”, need to parse it to a JSON object:

var json = $.parseJSON(data); (data example: {“tags”:[“14″,”13”]} )

json.tags[0] is 14

3 thoughts on “JSON Data Handling

  1. I cherished up to you will obtain performed proper here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get got an edginess over that you want be turning in the following. unwell unquestionably come further before again as precisely the similar just about a lot incessantly inside of case you defend this hike.

  2. Wow! Thank you! I continually needed to write on my blog something like that. Can I take a portion of your post to my website?

Comments are closed.