获得正确的JSON格式

时间:2012-07-12 08:10:19

标签: php mysql 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"
                           }
           }
}

所以下面的代码来自 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

更新:第3层:

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

2 个答案:

答案 0 :(得分: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"}]}}]

答案 1 :(得分: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
);