2012-07-12 83 views
3

所以我想製作一個JSON對象,該對象應該爲我提供一些關於某些問題的信息。這是我多麼希望它呈現一個僞代碼:獲取正確的JSON格式

{ 
"page" : 1, 
"info" : 
      { 
      "id" : 1, 
      "type" : 3, 
      "description": "How to do JSON?", 
      "alternatives" : 
          { 
          "id" : 1, 
          "description" : "Dunno" 
          } 
          { 
          "id" : 2, 
          "description" : "Let me show you" 
          } 
          { 
          "id" : 3, 
          "description" : "I refuse to show you" 
          } 
      } 
      "id" : 2, 
      "type" : 1, 
      "description": "Do you get it?", 
      "alternatives" : 
          { 
          "id" : 1, 
          "description" : "Yes" 
          } 
          { 
          "id" : 2, 
          "description" : "No" 
          } 
      } 
} 

因此,代碼下面是從夢魘(其中一個答案),它也正是我想用頁面做和問題,但我似乎無法弄清楚如何連接替代品到每個問題。你可以在我已經嘗試過的底部看到一段代碼,但是它沒有正確拼寫,現在我一直在對此進行仔細研究。

$before_json_encode[$row['page']][] = array(
    'id' => $row['id'], 
    'type' => $row['type'], 
    'description' => $row['description'], 
    'alternatives' => $alternativesarray//im not sure about here,dont know the structure of the alternative array 
); 

關於如何讓JSON數據的層次結構出現的另一個說明。我需要能夠挑選例如:在特定頁面上的所有問題的所有替代方案。因此,如果我想在我的民意調查中生成第3頁,我可以先在第3頁的子數組中找到問題,然後再從每個問題中獲取所有相關的替代方案。對不起,我的問題,窮人的解釋,它只是一個有點複雜:/

Page 
Question 
    Alternative 
    Alternative 
    Alternative 
Question 
    Alternative 
    Alternative 
    Alternative 
Question 
    Alternative 
    Alternative 
Page 
Question 
    Alternative 
    Alternative 
Question 
    Alternative 
    Alternative 

更新:第三層:

$rows_test2[$r['page']] 
['id' => $r['id'], 
'type' => $r['type'], 
'description' => $r['description']] 
[] = 
     array (
     'altid' => $t['altid'], 
     'altdesc' => $t['altdesc']); 
+0

你'$ alternativesarray []',這應該不會是'$ alternativesarray'或'$ alternativesarray [#編號]' – 2012-07-12 08:13:29

+0

「這並沒有真正的工作」 並不真正意味着什麼。它究竟如何不工作? – lanzz 2012-07-12 08:13:54

+0

從數據庫中獲取結果,然後從json_encode中將數組轉換爲Json格式。 http://php.net/manual/en/function.json-encode.php – Vimalnath 2012-07-12 08:15:00

回答

3
$rows[] = array(
    "page" => 1, 
    "info" => array(
     "id" => 1, 
     "type" => 3, 
     "description" => 'desc', 
    ) 
); 
echo json_encode($rows); // [{"page":1,"info":{"id":1,"type":3,"description":"desc"}}] 

更新:

$alternativesarray[]=array('id'=>'1', 'description'=>'yes'); 
$alternativesarray[]=array('id'=>'2', 'description'=>'no'); 
$rows[] = array(
    "page" => 1, 
    "info" => array(
     "id" => 2, 
     "type" => 3, 
     "description" => 'desc', 
     "alternatives" => $alternativesarray 
    ) 
); 
print json_encode($rows); // [{"page":1,"info":{"id":2,"type":3,"description":"desc","alternatives":[{"id":"1","description":"yes"},{"id":"2","description":"no"}]}}] 
+0

與靜態值不同,這與我所做的完全相同嗎?或者我錯過了什麼? – Tom 2012-07-12 08:34:10

+0

它應該是相同的,除非你錯過了什麼。 – 2012-07-12 08:35:29

+0

然後我們都錯了:)然後我們錯了:) – Tom 2012-07-12 08:38:53

1

也許是這樣嗎?

$before_json_encode[$row['page']][] = array(
     'id' => $row['id'], 
     'type' => $row['type'], 
     'description' => $row['description'], 
     'alternatives' => $alternativesarray//im not sure about here,dont know the structure of the alternative array 
); 
+0

'description'= $ row ['description']和其他選項包含'id','description'和'答案'。你可能會在那裏。 – Tom 2012-07-12 08:55:55

+0

抱歉,有關錯誤:) – Nightmare 2012-07-12 08:59:55

+0

你是我的救星!它非常完美!只需要弄清楚如何填充$ alternatives數組,就像我(你)現在所做的那樣。華麗的! – Tom 2012-07-12 09:01:07