2011-03-05 68 views
1

我是PHP中的新手(对于这个问题也是JSON),但我不确定/确信我拥有的json数据的格式。我将一个MySQL为JSON用于其他应用程序,而我得到的格式如下:PHP Json对数组进行编码

[ 
    { 
     "category": 
     { 
      "catId":"1", 
      "categoryName":"BABY FOOD", 
      "categoryNotes":"ONLY baby food varieties which have been listed below should be used. However if your doctor or pediatrician requires a specific diet please contact your Rabbi for further advice." 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"2", 
      "categoryName":"BAKING INGREDIENTS", 
      "categoryNotes":"" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"131", 
      "categoryName":"BEER", 
      "categoryNotes":"See Alcoholic Drinks" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"4", 
      "categoryName":"BEVERAGES - Powdered", 
      "categoryNotes":"" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"5", 
      "categoryName":"BISCUITS", 
      "categoryNotes":"Locally produced biscuits usually contain fats, oils or improvers of non-kosher origin. There is a very large range of Israeli, South African and American kosher biscuits available locally. Even these imported biscuits and crackers (including Israeli produced goods) should only be used if marked with a reliable Rabbinic Hechsher. A 'K' on the packet alone is insufficient. Please note which products are dairy or pareve." 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"6", 
      "categoryName":"BREAD AND BAKERY GOODS", 
      "categoryNotes":"Bread usually contains or comes in contact with Non-Kosher oils, fats or improvers and cannot be accepted as kosher without thorough investigation and subsequent authorisation" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"7", 
      "categoryName":"BREAD\/CORN\/RICE CRUMBS", 
      "categoryNotes":"" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"8", 
      "categoryName":"BUTTER AND DAIRY BLENDS", 
      "categoryNotes":"Soft butters, butter spreads & dairy blends are NOT ACCEPTABLE unless specifically listed. Butter made from whey cream is NOT ACCEPTABLE unless produced under Rabbinic supervision" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"9", 
      "categoryName":"BUTTERMILK and LEBEN ", 
      "categoryNotes":"" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"10", 
      "categoryName":"CAKES & CAKE SHOPS", 
      "categoryNotes":"Cakes and cake mixes usually contain Non-Kosher ingredients, and must only be used if they are produced under supervision <br\/> Cakes bought in a Non-Kosher cake shop, even if containing only kosher ingredients are NOT ACCEPTABLE because of the Non-Kosher utensils used in their preparation" 
     } 
    }, 
    { 
     "category": 
     { 
      "catId":"11", 
      "categoryName":"CEREALS ", 
      "categoryNotes":"" 
     } 
    }, 
    ... 
] 

等等。

现在,我不知道这是否是正确的格式,但我的代码来生成这个如下:根据我的代码

  <?php 


      /* soak in the passed variable or set our own */ 
      // $approved_prod_date = $_GET['date']; 



      $link = mysql_connect('localhost','root','broncos') or die('Cannot connect to the DB'); 
      mysql_select_db('iKosher',$link) or die('Cannot select the DB'); 

      /* grab the categories from the db */ 
      $query= "SELECT c.id as catId, c.name as categoryName,  c.notes as categoryNotes 
       FROM  cat c 
       WHERE  1=1\n"; 


      if(isset($_GET['catid']) && intval($_GET['catid'])) { 
       $query.="AND c.id = "; 
       $query.= $_GET['catid']; 
      } 


      if(isset($_GET['startIndex']) && intval($_GET['startIndex'])) { 
       if(isset($_GET['numItems']) && intval($_GET['numItems'])) { 
        $query .= "\nLIMIT "; 
        $query .= $_GET['startIndex']; 
        $query .= ","; 
        $query .= $_GET['numItems']; 
       } 
      } 




      // $query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts"; 
      $result = mysql_query($query,$link) or die('Errant query: '.$query); 
      $num=mysql_numrows($result); 


      /* create one master array of the records */ 
       $categories = array(); 
       if($num) { 
       while($category = mysql_fetch_assoc($result)) { 
        $categories[] = array('category'=>$category); 
       } 
       } 
       header('Content-type: application/json'); 
       echo json_encode($categories); 
      ?> 

,我这样做对吗?数组是否正确转换为JSON?

回答

6

json_encode()将产生有效的JSON。如有必要,您可以通过将JSON输出复制并粘贴到像http://www.jsonlint.com/

这样的在线验证器来验证这一点。如果您不确定数据是否正确放入数组中,并且正确地使用了组织,这是另一回事。但是,给定一个有效的PHP数组/对象/几乎任何东西,json_encode()都会生成以JSON格式正确编码的数组。

编辑:内部的while循环中,此代码:

 while($category = mysql_fetch_assoc($result)) { 
      $categories[] = array('category'=>$category); 
     } 

构建阵列看起来像这样:( “类别”=> [$ CATID,$类别名称等], “类别”= > [$ CATID,$类别名称,等],等等),我想你会更好(更快乐与生产的JSON),如果你把它改成:

 while($category = mysql_fetch_assoc($result)) { 
      $categories[] = $category; 
     } 

原始代码添加每个并将每个类别与具有相同“类别”键的$ categories数组相关联。但是由于这个密钥对于每个类别都是重复的,它没有真正的目的。

+0

我已经做到了这之前,它显示有效的,只是我已经感觉这个JSON是不正确无论如何。喜欢它应该有:[“类别”:{“catId”:“1”,“...而不是[{”类别“:{”catId“:”1“,”c – Doz 2011-03-05 10:29:02

+0

这是因为你的$类别是关联数组数组,并且在你的while循环中,你说“category”是包含catId,categoryName等的数组的关键。 – 2011-03-05 10:34:21

+0

扩展了解决上述注释的答案。 – 2011-03-05 10:40:40

0
if(isset($_GET['catid']) && intval($_GET['catid'])) { 
    $query.="AND c.id = "; 
    $query.= $_GET['catid']; 
} 

从来没有,只是从来没有这样做!先阅读关于SQL注入的一些信息!
您只需检查GET参数的intval是否不为0.
但是可以有任何东西!例如一个字符串“5工会选择版本(),用户(),等... - Q”