Xml jqgrid(邻接)中带json的treegrid,jqgrid不带json显示数据?

Xml jqgrid(邻接)中带json的treegrid,jqgrid不带json显示数据?,xml,json,jqgrid,treeview,Xml,Json,Jqgrid,Treeview,我很难找到如何将这个xml转换成json的例子,用于jqgrid的邻接模型,xml是可以工作的,但是在文档中没有json的例子,所以有人能指导我将这个xml转换成json吗???,我还在想办法,真的很感谢你的帮助 这是我的代码: if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) { header("Content-type: application/xhtml+xml;charset=utf-8");

我很难找到如何将这个xml转换成json的例子,用于jqgrid的邻接模型,xml是可以工作的,但是在文档中没有json的例子,所以有人能指导我将这个xml转换成json吗???,我还在想办法,真的很感谢你的帮助

这是我的代码:

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,但我在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);

        }
示例JSON(注意我们的ID是GUID):


也许您可以轻松转换。

如果您有任何问题,请告诉我
{"total":1,"page":1,"records":1,"rows":[{"id":"1","cell":["01","1001","Coordinador Distrital","ACBJ1975000001","JOSE RAUL","ACEVES","BARRIGA","0","0","false","false"]}]}
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);

        }
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;
    }
{
   "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
         ]
      }
   ]
}