3

所以我正在尝试制作一个 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"
                           }
           }
}

所以下面的代码来自Nightmare(答案之一),它完全符合我想要对页面和问题做的事情,但我似乎无法弄清楚如何将每个问题的替代方案联系起来。您可以在底部看到我尝试过的一个片段,但它拼写不正确,我已经为此苦苦挣扎了一段时间。

$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']);
4

2 回答 2

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"}]}}]
于 2012-07-12T08:17:33.860 回答
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
);
于 2012-07-12T08:52:56.743 回答