I want to format json
in this format for js
library! GoJs expect json in this format:
我想用这种格式为js库格式化json !GoJs希望json是这种格式:
model.nodeDataArray =
[
{ key: "1", username: "Don Meow", source: "cat1.png" },
{ key: "2", parent: "1", username: "Demeter", source: "cat2.png" },
{ key: "3", parent: "1", username: "Copricat", source: "cat3.png" },
{ key: "4", parent: "3", username: "Jellylorum", source: "cat4.png" },
{ key: "5", parent: "3", username: "Alonzo", source: "cat5.png" },
{ key: "6", parent: "2", username: "Munkustrap", source: "cat6.png" }
];
In php i try to return json like above but am realy new to json and my example dont work!
在php中,我试着返回像上面那样的json,但是对于json来说是全新的,我的示例不工作!
$users = $db->query("SELECT * FROM user");
$data = array();
while ($result = $users->fetch_assoc())
{
$data['key'] = $result['id'];
$data['username'] = $result['username'];
$data['email'] = $result['email'];
$data['parent'] = $result['parent'];
array_push($data, $result);
}
echo json_encode($data);
My JSON looks like this:
我的JSON是这样的:
{"key":"7","username":"Vlada","parent":"4","0":{"id":"1","parent":null,"username":"Ivan","email":"office.asd@gmail.com","password":"qwe123"},"1":{"id":"2","parent":"1","username":"Martinu","email":"asd@gmail.com","password":"qwe123"},"2":{"id":"3","parent":"1","username":"Biljana","email":"asd.com","password":"qwe123"},"3":{"id":"4","parent":"2","username":"Emil","email":"test@test.com","password":null},"4":{"id":"5","parent":"2","username":"Elena","email":"test@test.com","password":null},"5":{"id":"6","parent":"4","username":"Bole","email":null,"password":null},"6":{"id":"7","parent":"4","username":"Vlada","email":null,"password":null}}
{“关键”:“7”,“用户名”:“得到”,“父母”:“4”,“0”:{“id”:“1”,“父母”:空,“用户名”:“伊万”、“电子邮件”:“office.asd@gmail.com”,“密码”:“qwe123”}," 1 ":{ " id ":“2”,“父母”:“1”,“用户名”:“Martinu”、“电子邮件”:“asd@gmail.com”,“密码”:“qwe123”},“2”:{ " id ":“3”,“父母”:“1”,“用户名”:“普”、“电子邮件”:“asd.com”,“密码”:“qwe123”},“3”:{ " id ":“4”,“父母”:“2”,“用户名”:“埃米尔”、“电子邮件”:“test@test.com”,“密码”:零}," 4 ":{ " id ":“5”,“父母”:“2”,“用户名”:“埃琳娜”、“电子邮件”:“test@test.com”,“密码”:零},“5”:{ "id”:“6”、“父母”:“4”、“用户名”:“伯乐”、“电子邮件”:空,“密码”:零},“6”:{ " id ":“7”,“父母”:“4”,“用户名”:“得到”,“电子邮件”:空,“密码”:零} }
I try to replace id
with key
becouse GoJs need key
property defined. My json is so so different and i need to format output like above json?
我尝试用key替换id,因为GoJs需要定义key属性。我的json如此不同,我需要将输出格式化为json之上?
What i do wrong here ?
我做错了什么?
2 个解决方案
#1
2
You just need to change the way you store it in array
你只需要改变在数组中存储它的方式
$users = $db->query("SELECT * FROM user");
$data = array();
while ($result = $users->fetch_assoc())
{
$row = array (
"key" => $result['id'],
"username" => $result['username'],
"email" => $result['email'],
"parent" => $result['parent'],
);
array_push($data, $row);
}
echo json_encode($data);
#2
2
You are doing some wierd stuff in here
你在这里做一些没用的东西。
Try simplifying it to
尽量简化它
$users = $db->query("SELECT * FROM user");
$data = array();
while ($row = $users->fetch_assoc())
{
$t = array();
$t['key'] = $row ['id'];
$t['username'] = $row ['username'];
$t['email'] = $row ['email'];
$t['parent'] = $row ['parent'];
$data[] = $t;
}
Or even more simple, specify the columns you want in the query, which will make it quicker anyway and then you have your array ready built
或者更简单,在查询中指定您想要的列,这将使它变得更快,然后您就可以构建自己的数组了。
$users = $db->query("SELECT id,username,email,parent FROM user");
$data = array();
while ($row = $users->fetch_assoc())
{
$data[] = $row;
}
#1
2
You just need to change the way you store it in array
你只需要改变在数组中存储它的方式
$users = $db->query("SELECT * FROM user");
$data = array();
while ($result = $users->fetch_assoc())
{
$row = array (
"key" => $result['id'],
"username" => $result['username'],
"email" => $result['email'],
"parent" => $result['parent'],
);
array_push($data, $row);
}
echo json_encode($data);
#2
2
You are doing some wierd stuff in here
你在这里做一些没用的东西。
Try simplifying it to
尽量简化它
$users = $db->query("SELECT * FROM user");
$data = array();
while ($row = $users->fetch_assoc())
{
$t = array();
$t['key'] = $row ['id'];
$t['username'] = $row ['username'];
$t['email'] = $row ['email'];
$t['parent'] = $row ['parent'];
$data[] = $t;
}
Or even more simple, specify the columns you want in the query, which will make it quicker anyway and then you have your array ready built
或者更简单,在查询中指定您想要的列,这将使它变得更快,然后您就可以构建自己的数组了。
$users = $db->query("SELECT id,username,email,parent FROM user");
$data = array();
while ($row = $users->fetch_assoc())
{
$data[] = $row;
}