How to change name of JSON key?

爱⌒轻易说出口 提交于 2019-12-11 16:53:01

问题


children: [
{
    name:'Basic Ext Layouts',
    expanded: false,
    children:[
    {
        name:'Absolute',
        id:'absolute',
        leaf:true,
    },{
        ...
    }]
}]

Is it possible to change children to mydata?


回答1:


JSON format is merely a String in javascript's point of view. So you could manipulate the JSON string with associated method. 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);



回答2:


Is it possible to change children to mydata?

Yes. Setup treestore's proxy to use reader with root config set to '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',

Here is working example.



来源:https://stackoverflow.com/questions/9394507/how-to-change-name-of-json-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!