2011-03-05 113 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」