.NET Web サービスからの JSON 応答から読み込む ExtJS ツリー パネルを作成しています。応答を直接適用してツリーを表示すると、正しいjson応答が得られたようです。誰かが私が間違っているところを助けてくれます。以下は私のコードです:
Ext.onReady(function () {
var mystore = Ext.create('Ext.data.TreeStore',
{
autoLoad: true,
root: {
text: 'Root',
expanded: true//,
//id: 'ID'
},
proxy: {
type: 'ajax',
url: 'FirstService.asmx/GetTreeNodes',
reader:
{
type: 'json'
}
}
});
Ext.create('Ext.tree.Panel', {
title: 'Simple Tree',
width: 200,
height: 300,
useArrows: true,
rootVisible: false,
store: mystore,
renderTo: 'mydiv'
});
});
私のwebsreviceコードは次のようなものです:
public class TreeNode
{
public int ID { get; set; }
public string text { get; set; }
public bool leaf { get; set; }
public List<TreeNode> children { get; set; }
}
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
public void GetTreeNodes()
{
List<TreeNode> list = new List<TreeNode>();
for (int i = 1; i <= 4; i++)
{
TreeNode parent = new TreeNode();
parent.ID = i;
parent.text = "Nodes." + i;
parent.leaf = false;
parent.children = new List<TreeNode>();
for (int j = 1; j <= 2; j++)
{
TreeNode child = new TreeNode();
child.ID = i * 10 + j;
child.text = "Nodes." + child.ID;
child.leaf = true;
parent.children.Add(child);
}
list.Add(parent);
}
string jsonResult = new JavaScriptSerializer().Serialize(list.ToList());
//{"success": true,"root":[{"ID":1,"text":"Nodes.1","leaf":false,"children":[{"ID":11,"text":"Nodes.11","leaf":true,"children":null},{"ID":12,"text":"Nodes.12","leaf":true,"children":null}]},{"ID":2,"text":"Nodes.2","leaf":false,"children":[{"ID":21,"text":"Nodes.21","leaf":true,"children":null},{"ID":22,"text":"Nodes.22","leaf":true,"children":null}]},{"ID":3,"text":"Nodes.3","leaf":false,"children":[{"ID":31,"text":"Nodes.31","leaf":true,"children":null},{"ID":32,"text":"Nodes.32","leaf":true,"children":null}]},{"ID":4,"text":"Nodes.4","leaf":false,"children":[{"ID":41,"text":"Nodes.41","leaf":true,"children":null},{"ID":42,"text":"Nodes.42","leaf":true,"children":null}]}]}
//{ root: { expanded: true, children:
jsonResult = "{ root: { expanded: true, children: " + jsonResult + "}}";
System.Web.HttpContext.Current.Response.ContentType = "application/json";
System.Web.HttpContext.Current.Response.Write(jsonResult);
}
Myjsonの応答は、FFコンソールウィンドウに表示されるものです
{ root: { expanded: true, children: [{"ID":1,"text":"Nodes.1","leaf":false,"children":[{"ID":11,"text":"Nodes.11","leaf":true,"children":null},{"ID":12,"text":"Nodes.12","leaf":true,"children":null}]},{"ID":2,"text":"Nodes.2","leaf":false,"children":[{"ID":21,"text":"Nodes.21","leaf":true,"children":null},{"ID":22,"text":"Nodes.22","leaf":true,"children":null}]},{"ID":3,"text":"Nodes.3","leaf":false,"children":[{"ID":31,"text":"Nodes.31","leaf":true,"children":null},{"ID":32,"text":"Nodes.32","leaf":true,"children":null}]},{"ID":4,"text":"Nodes.4","leaf":false,"children":[{"ID":41,"text":"Nodes.41","leaf":true,"children":null},{"ID":42,"text":"Nodes.42","leaf":true,"children":null}]}]}}
ストアでこの応答を直接使用すると、ツリーが読み込まれます。ここで親切に助けてください。ありがとう