2014-10-28 122 views
0

我正在为Android应用程序构建管理后端。我的代码提供了一个json输出,然后android开发人员在他的应用程序中使用它。开发人员问我是否有可能减少层级的JSON输出从json输出中删除不必要的层次结构

json output

中删除突出[]0并直接把内容[]data,而不是内部。

这是我目前的PHP代码

if($type == 1)    //Handle item display 
{ 
    try 
    { 
     $query = "SELECT category FROM category";  
     $result= $DBH->query($query); 

     while($row = $result->fetch(PDO::FETCH_ASSOC)) 
     { 
      $cat = $row['category']; 
      $query1 = "SELECT * FROM item WHERE catagory='$cat'";  
      $value = $DBH->query($query1); 
      if($row1 = $value->fetchAll(PDO::FETCH_OBJ)){ 
       $main[] = array('data'=>array($row1)); 
      } 
      else 
      { 
       $main[] = array('data'=>array('catagory'=>$row['category'])); 
      } 

     } 
     echo json_encode($main); 
     $result->closeCursor();   //Close database connection free resources 
     $DBH = null; 
    } 
    catch(PDOException $e){ 
     print $e->getMessage(); 
     die(); 
    } 

} 

而产生

[ 
    { 
    "data": [ 
     [ 
     { 
      "id": "2", 
      "name": "rice", 
      "price": "20", 
      "description": "Plain Rice", 
      "image": "4106_rice.jpg", 
      "time": "12 mins", 
      "catagory": "Lunch", 
      "subcat": "" 
     }, 
     { 
      "id": "3", 
      "name": "item 1", 
      "price": "32", 
      "description": "item 1 description", 
      "image": "1370_2Ckjikljklkljh.jpg", 
      "time": "10", 
      "catagory": "Lunch", 
      "subcat": "Chicken Soup" 
     }, 
     { 
      "id": "4", 
      "name": "hello", 
      "price": "10", 
      "description": "fgsdjfsfsdj", 
      "image": "", 
      "time": "76", 
      "catagory": "Lunch", 
      "subcat": "" 
     } 
     ] 
    ] 
    }, 
    { 
    "data": { 
     "catagory": "Dinner" 
    } 
    }, 
    { 
    "data": { 
     "catagory": "Soup" 
    } 
    }, 
    { 
    "data": { 
     "catagory": "Test" 
    } 
    } 
] 

的JSON输出我不是很肯定,如果我可以让他问的变化,或者如果它是可能的。它可行吗?

回答

1

您的JSON结构是不一致的,并尝试这种方法,会更容易为开发者解析

{ 
    "response": [ 
     { 
      "data": [ 
       { 
        "id": "2", 
        "name": "rice", 
        "price": "20", 
        "description": "Plain Rice", 
        "image": "4106_rice.jpg", 
        "time": "12 mins", 
        "catagory": "Lunch", 
        "subcat": "" 
       }, 
       { 
        "id": "3", 
        "name": "item 1", 
        "price": "32", 
        "description": "item 1 description", 
        "image": "1370_2Ckjikljklkljh.jpg", 
        "time": "10", 
        "catagory": "Lunch", 
        "subcat": "Chicken Soup" 
       }, 
       { 
        "id": "4", 
        "name": "hello", 
        "price": "10", 
        "description": "fgsdjfsfsdj", 
        "image": "", 
        "time": "76", 
        "catagory": "Lunch", 
        "subcat": "" 
       } 
      ] 
     }, 
     { 
      "data": [ 
       { 
        "catagory": "Dinner" 
       } 
      ] 
     }, 
     { 
      "data": [ 
       { 
        "catagory": "Soup" 
       } 
      ] 
     }, 
     { 
      "data": [ 
       { 
        "catagory": "Test" 
       } 
      ] 
     } 
    ] 
}