我一直在寻找有关如何在Mysql查询中在JSON对象中创建JSON数组的解决方案。需要你们任何人的帮助。
我在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 |
------------------------------
我的MySQL查询:
选择p.firstname,p.lastname,p.rating,c.name作为子项 来自父p在p.id = c.idparent
上加入儿童cOutput:
-------------------------------------------------
| 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 |
-------------------------------------------------
这是我想要的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
}
];
我从生成JSON中遇到的那个是关于孩子的:[],我想要一个带双引号的数组""以逗号分隔,''在 - 的里面 []。
谢谢。
NB: 我正在使用codeigniter进行编码。如果您以前在codeigniter上遇到过这种问题,我期待着与您分享。
答案 0 :(得分:0)
$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;
提供以下输出:
[{"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}]
答案 1 :(得分:0)
我正在使用NEIL的阵列结构
在codeigniter中,最后使用以下代码进行编码。
from core import settings
它将输出以下内容:
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($your_array));
在AJAX返回时,使用$ .parseJSON(数据)来获取所需的结果。
答案 2 :(得分:0)
感谢所有人,感谢宝贵的回应。 我刚刚得到了解决方案。
通过使用mysql中的任何可用函数搜索任何解决方案(我使用的是json_extract()和group_concat()),但它没有像我想要的那样提供最佳格式。
受上述答案的启发, 我在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);
给出的结果:
[
{
"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
}
]