jQuery MiniUI

标题: josn数据转换 [打印本页]

作者: netui    时间: 2013-10-11 15:54:35     标题: josn数据转换

Factory版主:请教下,

按照 前面你说的方法:

我将对象的转为Json格式后 传回到后台去,格式如下:

[{"tm1":{"ID":"1","KSDA":"2","FLAG":"Y","FLAG_BZ":"test"},"tm2":{"ID":"2","KSDA":"1","FLAG":"Y","FLAG_BZ":"test"}}]

然后,在后台通过:ArrayList 去调用后台数据处理过程。

String json = Request["data"];
ArrayList rows = (ArrayList)JSON.Decode(json);

提示,类型转换错误:

System.InvalidCastException: 无法将类型为“System.Collections.Hashtable”的对象强制转换为类型“System.Collections.ArrayList”

想问下,要传回什么样格式的Json?

我另外有查看其它DataGrid传回的数据格式:

[{"PID":2,"TXMC":"单选题","NDFB":null,"TMSL":null,"MTFS":"2","ID":4,"SORT":1,"_id":45,"_uid":45,"_state":"modified"},{"PID":2,"TXMC":"简答题","NDFB":null,"TMSL":null,"MTFS":"5","ID":7,"SORT":1,"_id":48,"_uid":48,"_state":"modified"}]

和前面的也差不多的,




作者: factory    时间: 2013-10-11 16:23:36

String json = Request["data"];
ArrayList rows = (ArrayList)Test.JSON.Decode(json);
Hashtable aa = (Hashtable)rows[0];
Hashtable tm1 = (Hashtable)aa["tm1"];

string FLAG = tm1["FLAG"].toString();
作者: netui    时间: 2013-10-11 16:58:11

factory 发表于 2013-10-11 16:23
String json = Request["data"];
ArrayList rows = (ArrayList)Test.JSON.Decode(json);
Hashtable aa = (H ...

try
        {
            String json = Request["data"];

            ArrayList rows = (ArrayList)JSON.Decode(json);

            string sql=@"";
            int i = 1;
            Hashtable tm = new Hashtable();
            foreach (Hashtable row in rows)
            {
                tm=(Hashtable)row["tm"+i.ToString()];
                //sql += "update lxzls set ksda='" + tm["KSDA"].ToString() + "' flag='" + tm["FLAG"].ToString() + "' flag_bz='" + tm["FLAG_BZ"].ToString() + "' where id='" + tm["ID"].ToString() + "'\n";
                sql += "update lxzls set ksda='" + tm["KSDA"].ToString() + "' where id='" + tm["ID"].ToString() + "'\n";
                i++;
            }
            //new LXZL_DB().SaveLXZL(sql);
            //Response.Write("modified");
            Response.Write(sql);
        }
        catch (Exception e)
        {
            Response.Write(e.ToString());//主要是Session未登录的错误。
        }

前面我也是这样做的,

后面查找到原因,就是tm1对象FLAG我前台没有传进来。

感谢,现在可以了。
作者: netui    时间: 2013-10-11 17:07:48

netui 发表于 2013-10-11 16:58
try
        {
            String json = Request["data"];

上面的tm=(Hashtable)row["tm"+i.ToString()]; 都是无用的,其实里面就只有一行。

tm= (Hashtable)rows[0]; 即可。

非常感谢factory,重点难题解决了。




欢迎光临 jQuery MiniUI (http://miniui.com/discuss/) Powered by Discuz! X2