将JSON字符串放入商店 [英] Placing JSON string into a store

查看:158
本文介绍了将JSON字符串放入商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的简单问题。如何将此JSON字符串放入Ext.data.Store?

  {
elements:[
{
element:{
name:value 1,
id:element 1,
attributes:[
{
attrname:id,
attrvalue:这是ID
},
{
attrname: ,
attrvalue:这是名字
},
{
attrname:description,
attrvalue:这是描述
},
{
attrname:table_name,
attrvalue:这是表
}
]
}
}
}

这是一个简化版本我的问题:将JSON响应元素放入存储 - Ext JS



Cheers!

解决方案

由于JSON从服务器回来,你需要做的事情是将它从JSON格式的字符串反序列化为可以导航的对象。假设它是有效的JSON,你可以使用Ext.decode()简单地做到这一点。



这将给出一个在elements键上有一个对象数组的对象。简单地循环元素数组,并创建一个可以插入到您的商店中的新模型实例。



以下是一个直播示例的链接: https ://fiddle.sencha.com/#fiddle/3u6



这里是该示例中的代码:

  var data = YOUR_DATA_HERE,
var store = Ext.create('Ext.data.Store',{
fields:['id' 'name','description','table_name'],
proxy:{
type:'memory'
}
})
Ext.create .grid.Panel',{
title:'Test Data',
store:store,
columns:[{
text:'ID',
dataIndex: 'id'
},{
text:'Name',
dataIndex:'name'
},{
text:'Description',
dataIndex:'description'
},{
text:'Table Name',
dataIndex:'table_name'
}],
renderTo:Ext.getBody()
});
var decoding = Ext.decode(data);
//循环解码数据
for(var i = 0; i< decoded.elements.length; i ++){
var element = decoded.elements [i] .element;
//设置隐式模型
var model = {};
//循环遍历属性
for(var x = 0; x< element.attributes.length; x ++){
var attribute = element.attributes [x]
model [attribute.attrname] = attribute.attrvalue;
}
//隐含地将数据转换为模型
store.add(model);
}


Simple question here. How could I place this JSON string into an Ext.data.Store?

   {
   "elements":[
      {
         "element":{
            "name":"value 1",
            "id":"element 1",
            "attributes":[
               {
                  "attrname":"id",
                  "attrvalue":"This is the ID"
               },
               {
                  "attrname":"name",
                  "attrvalue":"This is the name"
               },
               {
                  "attrname":"description",
                  "attrvalue":"This is the description"
               },
               {
                  "attrname":"table_name",
                  "attrvalue":"This is the table"
               }
            ]
         }
      }
   }

This is a simplified version of my question: Placing JSON response elements into a store- Ext JS

Cheers!

解决方案

Since the JSON is coming back from the server, the first thing you'll need to do is to deserialize it from a JSON-formatted string into an object that you can navigate. Assuming that it's valid JSON, you can simply do this by using Ext.decode().

This will give an object that has an array of objects at the "elements" key. Simply loop over the elements array and create a new model instance that can be inserted into your store.

Here's a link to a live example: https://fiddle.sencha.com/#fiddle/3u6

And here's the code from that example:

var data = YOUR_DATA_HERE,
var store = Ext.create('Ext.data.Store', {
    fields: ['id', 'name', 'description', 'table_name'],
    proxy: {
        type: 'memory'
    }
})
Ext.create('Ext.grid.Panel', {
    title: 'Test Data',
    store: store,
    columns: [{
        text: 'ID',
        dataIndex: 'id'
    }, {
        text: 'Name',
        dataIndex: 'name'
    }, {
        text: 'Description',
        dataIndex: 'description'
    }, {
        text: 'Table Name',
        dataIndex: 'table_name'
    }],
    renderTo: Ext.getBody()
});
var decoded = Ext.decode( data );
// loop over decoded data
for( var i=0; i<decoded.elements.length; i++ ) {
    var element = decoded.elements[ i ].element;
    // set implicit model
    var model = {};
    // loop over attributes
    for( var x=0; x<element.attributes.length; x++ ) {
        var attribute = element.attributes[ x ];
        model[ attribute.attrname ] = attribute.attrvalue;
    }
    // implicitly cast data as Model
    store.add( model );
}

这篇关于将JSON字符串放入商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆