从Ext.data.Store转换jsondata到列表对象

时间:2012-01-03 16:55:51

标签: c# asp.net-mvc extjs

我有一个用户创建按钮,用于将数据从商店发送到服务器端c#代码。

    // send data through link
    function valideit() {
        var jsonData = Ext.encode(Ext.pluck(store.data.items, 'data'));
        Ext.Ajax.request({
            url: '/Contact/Validate', params: { data: jsonData },
            success: function (result, request) {
                Ext.MessageBox.alert('Success', 'Data return from the server: ' + result.responseText);
            },
            failure: function ( result, request) { 
                Ext.MessageBox.alert('Failed', 'Successfully posted form: '+result.date); 
            } 
        });
    }

和C#:

    [HttpPost]
    public JsonResult Validate(object data)
    {
        return Json(new
        {
            success = "This worked", failure = "Doesnt work"
        },JsonRequestBehavior.AllowGet);
    }

这是我从"数据"

获得的数据

数据=%5B%7B%22%C3%9AtgefandiKennitala%22%3A%224911069990%22%2C%22%C3%9AtgefandiHeiti%22%3A%22Landsbanki%20Absolute%20Return%20Stra%22%2C%22Rekstrarf% C3%A9lagKennitala%22%3A%227005942549%22%2C%22Rekstrarf%C3%A9lagNafn%22%3A%22Landsvaki%20hf。%22%2C%22ISIN%22%3A%22IS0000016913%22%2C%22Au%C3%B0kenni %22%3A%22LARS%22%2C%22Tegundheiti%22%3A%22Hlutdeildarsk%C3%ADrteinioghlutir%22%2C%22Skr%C3%A1%C3%B0heiti%22%3A%22%C3%93skr%C3%A1 %C3%B0%22%2C%22Me%C3%B0all%C3%ADFT%C3%ADMI%22%3A%220%22%2C%22Fj%C3%B6ldi%22%3A2101%2C%22Gengi%22%3A0 0.860383%2C%22Gjaldmi%C3%B0ill%22%3A%22USD%22%2C%22GengiGjaldmi%C3%B0ils%22%3A%22117.59%22%2C%22B%C3%B3kf%C3%A6rtVer%C3%B0 %22%3A%22212.53%22%2C%22Athugasemd%22%3A%22%22%7D%2C%7B%22%C3%9AtgefandiKennitala%22%3A%226105109810%22%2C%22%C3%9AtgefandiHeiti%22 %3A%22LFESTI%20Borgart%C3%禁令%22%2C%22Rekstrarf%C3%A9lagKennitala%22%3A%22%22%2C%22Rekstrarf%C3%A9lagNafn%22%3A%22%22%2C%22ISIN%22 %3A%22IS0000020105%22%2C%22Au%C3%B0kenni%22%3A%22LFEST1%2010%201%22%2C%22Tegundheiti%22%3A% 22%C3%96nnurver%C3%B0br%C3%A9F%22%2C%22Skr%C3%A1%C3%B0heiti%22%3A%22Skr%C3%A1%C3%B0%22%2C%22Me%C3% B0all%C3%ADFT%C3%ADMI%22%3A%2212.32%22%2C%22Fj%C3%B6ldi%22%3A984567%2C%22Gengi%22%3A1.06348%2C%22Gjaldmi%C3%B0ill%22% 3A%22ISK%22%2C%22GengiGjaldmi%C3%B0ils%22%3A%221%22%2C%22B%C3%B3kf%C3%A6rtVer%C3%B0%22%3A%221047.07%22%2C%22Athugasemd% 22%3A%22%22%7D%2C%7B%22%C3%9AtgefandiKennitala%22%3A%224603069490%22%2C%22%C3%9AtgefandiHeiti%22%3A%22Land


那么如何才能以可用的格式获取这些数据?

谢谢

2 个答案:

答案 0 :(得分:3)

嗨我想在得到杰森的帮助后回答我自己的问题。

在客户端,你有STORE.data.items中的数据(在我的例子中是ExtJS网格)

这是按下按钮时的动作:

function valideit() {
    var jsonData = Ext.encode(Ext.pluck(store.data.items, 'data'));
    Ext.Ajax.request({
        url: '/Contact/Validate', params: { data: jsonData },
        success: function (result, request) {
            Ext.MessageBox.alert('Success', 'Data return from the server: ' + result.responseText);
        },
        failure: function (result, request) {
            Ext.MessageBox.alert('Failed', 'Successfully posted form: ' + result.date);
        }
    });
}

在服务器端:

[HttpPost]
            public JsonResult Validate(string data)
            {
                var response = HttpUtility.UrlDecode(data);


                List<Investments> myDeserializedObjList = (List<Investments>)Newtonsoft.Json.JsonConvert.DeserializeObject(data, typeof(List<Investments>));


                return Json(new
                {
                    success = "This worked!"
                },JsonRequestBehavior.AllowGet);
            }

答案 1 :(得分:2)

使用System.Web.HttpUtility.UrlDecode将数据更改为非转义字符串,然后使用Json.NET库将JSON字符串反序列化为c#对象