日期:2014-05-17  浏览次数:20442 次

如何解析这个的JSON 文件?
{
"table_1":
[
{"userID":"XXX","Device_Model":"AAA","Network":"YYY","System_Model":"ZZZ"},
{"userID":"XXX","Device_Model":"AAA","Network":"YYY","System_Model":"XXX"},
{"userID":"XXX","Device_Model":"AAA","Network":"YYY","System_Model":"XX"}
],

"table_2":
[
{"userID":"DDD","Ins_App":"XXX","Ins_Time":"2012-09-11","App_Version":"XXX","Add_App":"XXX"},
{"userID":"DDD","Ins_App":"XXX","Ins_Time":"2012-09-12","App_Version":"XXX","Add_App":"XXX"}
],

"table_3":
[
{"userID":"AAA","Pro_App_Name":"BB","OpenTime":"CCC","App_Version_1":"DDD"},
{"userID":"CCC","Pro_App_Name":"BB","OpenTime":"CCC","App_Version_1":"DDD"}
],

"table_4":
[
{"userID":"AAA","Browser_Model":"BB","URL":"CCC","OpenTime_1":"DDD","CloseTime":"DDD"},
{"userID":"AAA","Browser_Model":"BB","URL":"CCC","OpenTime_1":"DDD","CloseTime":"DDD"}
]
}


客户端会用HTTP POST 的形式发过来JSON 数据,涉及到四张表(table_1,table_2,table_3,table_4),结构是固定的,请问这么在后台获取数据,并且解析这个JSON 文件,然后存储到数据库(这个我会)

.NET 的JSON 处理实在不会,在网上找了好多教程,我总感觉没有那么复杂,这个像XML 一样结构是比较固定的,可不可以不写一个类,直接在后台解析数据?

最好写一点代码,我实在是对json和.net刚入门。。谢谢各位了。

------解决方案--------------------
 json = json.Trim();
        if (json[0] != '[')
            json = "[" + json;
        if (json[json.Length - 1] != ']')
            json = json + "]";

        JavaScriptSerializer jss = new JavaScriptSerializer();
        ArrayList arrList = jss.Deserialize<ArrayList>(json);
        if (arrList.Count > 0)
        {
            foreach (Dictionary<string, object> arr in arrList)
            {
                  foreach (string key in arr.Keys)
                  {
                            //key就是属性
                                 //arr[key]就是对应的属性值
                  }
            }
        }