简体   繁体   English

如何使用php生成json响应

[英]How to generate json response using php

How to generate json response using php 如何使用php生成json响应

In the model: 在模型中:

public function groups($getGroupId) {
        $cols = array('group_id','name');
        $sql = $this->select ()
                    ->from ( $this->_name, $cols )
                    ->where ( 'parent_id=?', $getGroupId );
        $groupDetails = $this->fetchAll ( $sql );
        //$childGroupName = $groupDetails['name'];
        return $groupDetails;
}

groupDetails.php page: groupDetails.php页面:

$dbGroup = new dbGroups();
$groupDetails = $dbGroup -> groups($getGroupId);
$jsonResponse = json_encode($groupDetails);
print_r($jsonResponse);

When printing the data i'm getting response like this 打印数据时,我得到这样的响应

[{"group_id":"2","name":"ABCD"},{"group_id":"7","name":"XYZ"}]  

But i want output like this, Because i have to generate a jstree using json 但我想要这样的输出,因为我必须使用json生成一个jstree

[
   {
      "data" : {
         "icon" : <optional>,
         "title" : <node name>
      },
      "attr" : {
         "rel" : <the type you defined in the js (maybe "group")>,
         "title" : <node title>,
         "id" : <the node's id / group id>
      },
      "state" : "closed"
   }
]

I would recommend that you use the output from json_encode as it is. 我建议您按原样使用json_encode的输出。 Takes less bandwidth. 占用较少的带宽。 Only reason I see for all the whitespace is for debugging, and for that I'd rather use FireBug and/or JSONView in FireFox. 我看到所有空白的唯一原因是调试,为此我宁愿在FireFox中使用FireBug和/或JSONView。

Anyways, if you really want to, you can maybe try the JSON_PRETTY_PRINT flag? 无论如何,如果您确实愿意,您可以尝试JSON_PRETTY_PRINT标志吗? Seems this was added in 5.4.0 though, so maybe not the version you're on supports it... There seems to be options you can use for that in the comments there though. 似乎这是在5.4.0中添加的,所以也许您所使用的版本不支持它。。。似乎在其中的注释中可以使用一些选项。 Maybe you can find something useful? 也许你能找到有用的东西? http://www.php.net/manual/en/function.json-encode.php#102091 http://www.php.net/manual/en/function.json-encode.php#102091


You say you have to create a jstree now, and that doesn't really have anything to do with what you're asking. 您说您必须立即创建一个jstree,而这实际上与您的要求没有任何关系。 You're two examples of data doesn't look anything alike at all. 您是两个数据完全不同的例子。 json_encode does not do anything special or magic. json_encode没有做任何特殊或魔术。 It just takes data and turns it into JSON. 它只需要数据并将其转换为JSON。 It's your job to make that data look correctly first, before encoding it. 使数据正确编码是您的工作。 Your DB query most likely returns a set of flat rows, and you'll have to loop through it and somehow generate your tree the way you want it. 您的数据库查询最有可能返回一组平面行,您将不得不循环遍历它,并以某种方式按您希望的方式生成您的树。 You can probably find other questions here about how to create tree structures out of flat DB results. 您可能会在这里找到有关如何根据平面数据库结果创建树结构的其他问题。

Since you are using Zend Framework, I recommend you to use Zend_Json . 由于您使用的是Zend Framework,我建议您使用Zend_Json Zend_Json is a pretty useful component to use in order to format Json from any supported format (object, array, xml...). Zend_Json是一个非常有用的组件,用于从任何支持的格式 (对象,数组,xml ...)格式化Json

Zend_Json::decode() and Zend_Json::encode() will allow you to encode and decode Json and prettyPrint() is used to make your output prettier. Zend_Json::decode()Zend_Json::encode()将允许您对Json进行编码和解码,而prettyPrint()用于使您的输出更漂亮。


Edit: As Svish said, your two examples doesn't look alike, so it's kind of hard to guess what you want to put inside your tree. 编辑:正如Svish所说的,您的两个示例看起来并不相似,因此很难猜测您想在树中放入什么。

What you need is to create your own array , so you can make it look like the way you want. 您需要创建自己的数组 ,以便使其看起来像您想要的方式。

For example, let's say you only want one row from your database in your tree, then your array would be something like this: 例如,假设您只需要树中数据库中的一行,那么您的数组将是这样的:

$v = array(
       array(
         "data" => array("icon" => "ICON",
                         "title" => $row->name),
         "attr" => array("rel" => "REL",
                         "title" => "TITLE", 
                         "id" => $row->group_id),
          "state" =>     "closed"));
echo Zend_Json::encode($v);

These lines should echo something like in your examples. 这些行应该像你的例子中那样回应。

To make it works with your fetchAll() , a simple foreach will do it. 为了使它适用于你的fetchAll() ,一个简单的foreach将会这样做。

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

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