在PHP中解析和构建JSON数组

   在PHP 5.2之后,PHP提供了对JSON数据的支持,分别是以下两个函数    $json_obj = json_decode($json_str) #把JSON字符串数据解析成对象    $json_str = json_encode($json_obj) #把对象转换成字符串 用过easyui同学可能都知道,easyui的datagrid接收是json的数据结构是这样的 {"total" : 2 , "rows" : [{"name" : "Herow","id": 123}, {"name" : "happy" , "id" : 321}] } 而php中用array("name" => "Herow")构建形成的JSON格式是都是包含在花括号{}中的,网上查了一下,找到了一种解决方法 解析JOSN $json_str = '[{"name" : "Herow","id": 123},{"name" : "happy" , "id" : 321}]'; $json_obj = json_decode($json_str); foreach($json_obj as $json_item){ echo $json_item->name."+".$json_item->id; } 构建JSON #{"total" : 2 , "rows" : [{"na