获取正确的JSON格式 [英] Getting correct JSON format

查看:119
本文介绍了获取正确的JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试制作一个JSON对象,该对象应该为我保存一些有关某些问题的信息.这是我希望如何显示的伪代码:

So I am trying to make a JSON object that should hold some information about some questions for me. This is a pseudo-code of how I want it to be presented:

{
 "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 (答案之一),它确实满足了我对页面和问题的要求,但我似乎无法弄清楚将替代方案与每个问题联系起来.您可以在我尝试过的底部看到一个代码段,但拼写不正确,并且我已经对此进行了一段时间的尝试.

So the code underneath is from Nightmare (one of the answers), and it does exactly what I want to do with the page and the questions, but I can't seem to figure out how to connect alternatives to each question. You can see a snippet at the bottom where I have tried but it's not correctly spelled and I've been hammering at this for a while now.

$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页子数组中找到问题,然后再从每个问题中访问该问题自己的子数组中所有已连接的替代项.抱歉,我的问题解释得不好,只是有点复杂://

Another illustration on how I want the hierarchy of the JSON data to appear. I need to be able to pick for instance: All alternatives to all questions on a specific page. So if I want to generate page 3 in my poll I can first find the questions within the page 3 sub-array, and then again from each question gain access to all of it's connected alternatives in that questions's own sub-array. Sorry for the poor explanation of my issue, it's just a bit complicated :/

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

推荐答案

$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"}]}}]

这篇关于获取正确的JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆