1

我在寻找如何在邻接模型中为 jqgrid 转换 json 中的 xml 的示例时遇到了麻烦,xml 可以工作,但是在文档中没有 json docs中的示例,所以有人可以指导我在 json 中转换这个 xml 吗? ?,我还在想办法怎么做,帮助真的很感激

这是我的代码:

if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
        header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
        header("Content-type: text/xml;charset=utf-8");
}
if(isset($_REQUEST['nodeid'])==FALSE) {
    $node =0;
}
else{
    $node = (integer)$_REQUEST["nodeid"];
}
$clsJqGrid = new redCuidadana();
$et = ">";
echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>1</page>";
echo "<total>1</total>";
echo "<records>1</records>";

if($node >0) { //check to see which node to load
   $wh = 'parent_id='.$node; // parents
   //$n_lvl = $n_lvl+1; // we should ouput next level
} else {
   //$wh = 'ISNULL(parent_id)'; 
   $wh ='parent_id=0';// roots
}
$mostrarRegistros= $clsJqGrid->mostrarRegistros($wh);
foreach ($mostrarRegistros as $row){
   echo "<row>";         
   echo "<cell>".$row["id_ciudadano"]."</cell>";
   echo "<cell>".$row["distrito"]."</cell>";
   echo "<cell>".$row["seccion"]."</cell>";
   echo "<cell>".$row["rol"]."</cell>";
   echo "<cell>".$row["clave_electoral"]."</cell>";
   echo "<cell>".$row["nombre"]."</cell>";
   echo "<cell>".$row["apaterno"]."</cell>";
   echo "<cell>".$row["amaterno"]."</cell>";
   echo "<cell>".$row["level"]."</cell>";
   echo "<cell><![CDATA[".$row["parent_id"]."]]></cell>";
   if($row["level"] == "2"){echo "<cell>"."true"."</cell>";}
   else{echo "<cell>".$row["isLeaf"]."</cell>";}
   echo "<cell>".$row["expanded"]."</cell>";
   echo "</row>";
}
echo "</rows>";
?>

这就是我到目前为止所拥有的

    <?php
require_once '../../sesion/model/clsSesion.php';
require_once '../../red_ciudadana/model/modelRedCuidadana.php';

if(isset($_REQUEST['nodeid'])==FALSE) {
    $node =0;
}
else{
    $node = (integer)$_REQUEST["nodeid"];
}

if($node >0) {
    $wh = 'parent_id='.$node;
    }
    else{

    $wh ='parent_id=0';
    }


header("Content-type: text/html;charset=utf-8");

$response->total = 1;
$response->page = 1;
$response->records = $count=1;
$i=0;

$clsJqGrid = new redCuidadana();
$mostrarRegistros= $clsJqGrid->mostrarRegistros($wh);

foreach ($mostrarRegistros as $row){

    if($row["level"] == "2"){$cosa= "true";}
        else{$cosa=$row["isLeaf"];}

     $response->rows[$i]['id']=$row["id_ciudadano"];
     $row["expanded"];
     $response->rows[$i]['cell']= array($row["distrito"],
     $row["seccion"],
     $row["rol"],
     $row["clave_electoral"],
     $row["nombre"],
     $row["apaterno"],
     $row["amaterno"],
     $row["level"],
     $row["parent_id"],
     $cosa, 
     $row["expanded"]
     );

    $i++;
}

echo json_encode($response);

?>

但是 jqgrid 没有显示数据,这是我得到的 json

{"total":1,"page":1,"records":1,"rows":[{"id":"1","cell":["01","1001","Coordinador Distrital","ACBJ1975000001","JOSE RAUL","ACEVES","BARRIGA","0","0","false","false"]}]}
4

1 回答 1

2

我不知道 php,但这就是我在 c# (ASP.NET MVC3) 中的做法:这是用于自动加载树,而不是一次加载树。

动作方法:

public JsonResult GetSPTreeJSON(string nodeid, string n_level, string currentUser)
        {
            List<TreeNode> list = _clientService.GetTreeNodeList(nodeid, n_level);

            var jsonData = new
            {
                page = 1,
                total = 1,
                records = 1,
                rows = (
                    from TreeNode u in list
                    select new
                    {
                        cell = new object[] { u.Id.ToString(), u.name, u.level, u.ParentId, u.isLeaf, false }
                    }).ToList()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);

        }

建立TreeNodes列表的方法:

public List<TreeNode> GetTreeNodeList(string nodeid, string n_level)
    {
        List<Tree> root = null;

        Guid? currentNode;
        if (nodeid == null)
            currentNode = null;
        else
            currentNode = new Guid(nodeid);

        if (nodeid == null) 
            root = ssdsContext.Trees.Where(x => x.ParentId == null).ToList(); 
        else
            root = ssdsContext.Trees.Where(x => x.ParentId == currentNode).ToList();

        List<TreeNode> list = new List<TreeNode>();

        int newLevel = n_level == null ? 0 : Convert.ToInt32(n_level) + 1;

        foreach (Tree t in root)
        {
            TreeNode n = new TreeNode
            {
                expanded = false,
                Id = t.UserId,
                isLeaf = t.IsUser,
                name = t.Name,
                ParentId = t.ParentId,
                level = newLevel
            };
            list.Add(n);
        }
        return list;
    }

树节点类:

public class TreeNode
    {
        public Guid Id;
        public string name;
        public int level;
        public Guid? ParentId;
        public bool isLeaf;
        public bool expanded;
    }

示例 JSON(注意我们的 id 是 GUIDS):

{
   "page":1,
   "total":1,
   "records":1,
   "rows":[
      {
         "cell":[
            "29846baa-ede0-4582-b9ed-39bcc486f2c2", //Id
            "Level1 Group", //Name
            1, //Level
            "5a1a9742-3e0f-11d3-941f-006008032006", //Parent Id
            false, //Is Leaf
            false //Expanded
         ]
      },
      {
         "cell":[
            "fd62f214-e25c-4d68-aa38-6805adfb4305",
            "Level1 Group",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            false,
            false
         ]
      },
      {
         "cell":[
            "39cc2783-811f-4885-9af6-d35b1a1385a2",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      },
      {
         "cell":[
            "5a1a9743-3e0f-11d3-941f-006008032006",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      },
      {
         "cell":[
            "466d78df-6f39-43ff-abfd-58e7bf91f8c8",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      },
      {
         "cell":[
            "c80ca2d1-8210-4c11-8c6d-005c97fce9cb",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      },
      {
         "cell":[
            "d1f870ed-bca6-4cc8-8b96-b19fa251c2f8",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      },
      {
         "cell":[
            "3061ce07-bff6-4d9a-a84a-a8a7d47ebd7a",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      },
      {
         "cell":[
            "d9dadce8-a9ce-4343-a8a0-b80844aa36ad",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      },
      {
         "cell":[
            "9042ded8-09ee-46f9-beac-8746ed1cdd17",
            "Level 1 Node",
            1,
            "5a1a9742-3e0f-11d3-941f-006008032006",
            true,
            false
         ]
      }
   ]
} 

也许你会很容易转换。

于 2012-08-14T11:23:28.850 回答