简体   繁体   English

如何在JSON数组Codeigniter中创建JSON数组

[英]How to create json array in json array codeigniter

I have been searching for the solution on how to create JSON array in JSON object from Mysql query. 我一直在寻找有关如何从Mysql查询在JSON对象中创建JSON数组的解决方案。 Need help from any of you. 需要任何人的帮助。

My source data in mysql: 我在mysql中的源数据:

Table: parent
--------------------------------------
| id | firstname | lastname | rating |
--------------------------------------
|  1 | John      | Doe      | 9.3    |
|  2 | Marry     | Jane     | 8.5    |
|  3 | Paijo     | Masni    | 9.8    |
--------------------------------------

Table: children
------------------------------
| id |  idparent  | name     |
------------------------------
|  1 |  1         | John A   |
|  2 |  1         | John B   |
|  3 |  1         | John C   |
|  4 |  2         | Jane A   |
|  5 |  2         | Jane B   |
|  6 |  3         | Bang Jo  |
|  7 |  3         | Kak Jo   |
------------------------------

My MySQL Query: 我的MySQL查询:

Select p.firstname, p.lastname, p.rating, c.name as children 选择p.firstname,p.lastname,p.rating,c.name作为子级
from parent p join children c on p.id = c.idparent 从父母p加入孩子c在p.id = c.idparent

Output:
-------------------------------------------------
| id | firstname | lastname | rating | children |
-------------------------------------------------
|  1 | John      | Doe      | 9.3    | John A   |
|  1 | John      | Doe      | 9.3    | John B   |
|  1 | John      | Doe      | 9.3    | John C   |
|  2 | Marry     | Jane     | 8.5    | Jane A   |
|  2 | Marry     | Jane     | 8.5    | Jane B   |
|  3 | Paijo     | Masni    | 9.8    | Bang Jo  |
|  3 | Paijo     | Masni    | 9.8    | Kak Jo   |
-------------------------------------------------

Here is my output of JSON format that I wanted: 这是我想要的JSON格式的输出:

var profile = [
    {
        "firstname": "John",
        "lastname": "Doe",
        "rating": 9.3,
        "children": [
            "John A",
            "John B",
            "John C"
        ],
        "id": 1
    },
    {
        "firstname": "Marry",
        "lastname": "Jane",
        "rating": 8.5,
        "children": [
            "Jane A",
            "Jane B"
        ],
        "id": 2
    },
    {
        "firstname": "Paijo",
        "lastname": "Masni",
        "rating": 9.8,
        "children": [
            "Bang Jo",
            "Kak Jo"
        ],
        "id": 3
    }
];

The one I got stuck from generating the JSON is on the children: [], I want to have an array with double-quote "" separated by comma ',' inside the []. 我因生成JSON而陷入困境的子项是在子项上:[],我想在[]内使用双引号“”以逗号“,”分隔的数组。

Thank you. 谢谢。

NB: Currenty I am using codeigniter for my coding. 注意:目前,我正在使用codeigniter进行编码。 If you have faced this kind of problem on codeigniter before, I'm looking forward to getting the sharing from you. 如果您以前在codeigniter上遇到过此类问题,我期待与您分享。

$people = array();
$person = array();

$person['firstname'] = "John";
$person['lastname'] = "Doe";
$person['rating'] = 9.3;
$person['children'] = array('John A', 'John B', 'John C');
$person['id'] = 1;

$people[] = $person;
$person = array();

$person['firstname'] = "Marry";
$person['lastname'] = "Jane";
$person['rating'] = 8.5;
$person['children'] = array('JaneA', 'JaneB');
$person['id'] = 2;

$people[] = $person;

$profile = json_encode($people);

echo $profile;

Gives the following output: 提供以下输出:

[{"firstname":"John","lastname":"Doe","rating":9.3,"children":["John A","John B","John C"],"id":1},
{"firstname":"Marry","lastname":"Jane","rating":8.5,"children":["JaneA","JaneB"],"id":2}]

I am using NEIL's array structure, 我正在使用NEIL的数组结构,

In codeigniter, use the following code at the end for encoding. 在codeigniter中,最后使用以下代码进行编码。

$this->output->set_content_type('application/json'); $this->output->set_output(json_encode($your_array));

It will output the following: 它将输出以下内容:

"[{\\"firstname\\":\\"John\\",\\"lastname\\":\\"Doe\\",\\"rating\\":9.3,\\"children\\":[\\"John A\\",\\"John B\\",\\"John C\\"],\\"id\\":1},{\\"firstname\\":\\"Marry\\",\\"lastname\\":\\"Jane\\",\\"rating\\":8.5,\\"children\\":[\\"JaneA\\",\\"JaneB\\"],\\"id\\":2}]"

And in AJAX return, use the $.parseJSON(data) to get your required result. 在AJAX返回中,使用$ .parseJSON(data)获得所需的结果。

Thanks, all, for the valuable responses. 谢谢大家的宝贵宝贵意见。 I just got the solution. 我刚找到解决方案。

After searching for any solution by using any available function in mysql (which I have used are json_extract() and group_concat() ), yet it didn't give the best format like what I wanted. 在使用mysql中的任何可用函数(我使用的是json_extract()和group_concat())搜索任何解决方案之后,它并没有提供我想要的最佳格式。

Inspired by the answers above, I have made my code like this in Codeigniter that works perfectly: 受以上答案的启发,我在Codeigniter中使我的代码完美运行:

$parent   = array();
$children = array();
$profile  = array();

$parent_query = $this->db->query("select firstname, lastname, rating, id from parent")->result_array();

for($i = 0; $i < count($parent_query); $i++)
{
   $children_query = $this->db->query("select name from children where idparent = '$parent_query[$i]['id']'")->result_array();

   $parent[$i]['firstname'] = $parent_query[$i]['firstname'];
   $parent[$i]['lastname']  = $parent_query[$i]['lastname'];
   $parent[$i]['rating']    = $parent_query[$i]['rating'];

   for($j = 0; $j < count($children_query); $j++)
   {
      $children[] = $children_query[$j]['name'];
   }

   $parent[$i]['children']  = $children;
}

$profile = json_encode($parent);

The result given: 给出的结果:

[  
   {  
      "firstname":"John",
      "lastname":"Doe",
      "rating":9.3,
      "children":[  
         "John A",
         "John B",
         "John C"
      ],
      "id":1
   },
   {  
      "firstname":"Marry",
      "lastname":"Jane",
      "rating":8.5,
      "children":[  
         "Jane A",
         "Jane B"
      ],
      "id":2
   },
   {  
      "firstname":"Paijo",
      "lastname":"Masni",
      "rating":9.8,
      "children":[  
         "Bang Jo",
         "Kak Jo"
      ],
      "id":3
   }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM