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

更多相关文章

  1. ubuntu下的Samba配置:使每个用户可以用自己的用户名和密码登录自

随机推荐

  1. android 字体的使用
  2. Android中TextVIew一些属性
  3. [置顶] Android(安卓)开发中Parcel存储类
  4. Android安全加密:消息摘要Message Digest
  5. Android的布局方法
  6. Android Support兼容包详解
  7. Android美工之Shape
  8. Android(安卓)处理屏幕旋转
  9. 桌面小控件(Widget)--之清理进程
  10. Android(安卓)修改包名无法运行activity