children: [
{
name:'Basic Ext Layouts',
expanded: false,
children:[
{
name:'Absolute',
id:'absolute',
leaf:true,
},{
...
}]
}]
是否可以將children
更改爲mydata
?如何更改JSON密鑰的名稱?
children: [
{
name:'Basic Ext Layouts',
expanded: false,
children:[
{
name:'Absolute',
id:'absolute',
leaf:true,
},{
...
}]
}]
是否可以將children
更改爲mydata
?如何更改JSON密鑰的名稱?
從JavaScript的角度來看,JSON格式僅僅是一個字符串。所以你可以用關聯的方法來操作JSON字符串。 JSON。
// The original
obj = {
children: [
{
name:'Basic Ext Layouts',
expanded: false,
children:[
{
name:'Absolute',
id:'absolute',
leaf:true,
}]
}]
}
// Transfer the object to a JSON string
var jsonstr = JSON.stringify(obj);
// HERE you do the transform
var new_jsonstr = jsonstr.replace('"children"', '"mydata"');
// You probably want to parse the altered string later
var new_obj = JSON.parse(new_jsonstr);
是否有可能改變孩子MYDATA?
是的。設置treestore的代理使用閱讀器root
配置設置爲'mydata'
:
var store = Ext.create('Ext.data.TreeStore', {
model: 'MyModel',
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'mydata' // << this is required
}
},
root: {
myData: [
{
name:'Basic Ext Layouts',
這裏是working example。
我想從外面添加json,我不希望它在同一個文件中,根據工作示例中的指定。 – Dhananjay 2012-02-23 05:57:59
如果數據包含字符串「children」出於某種原因,這可能有可怕的連續性。至少應該使用正則表達式 – 2012-06-19 02:29:33
謝謝。嚴重的問題在這裏。我在這裏用一個替換的''children''來更新,只是在JSON鍵上而不是一個普通的字符串。唯一的例外是這裏應該注意的普通字符串「children」。 – steveyang 2012-06-21 02:56:47
垃圾!爲什麼這是被接受的答案?這樣的黑客 – 2012-09-05 22:26:17