none
C#,没有key只有value的JSON串,如何转换为list<T> RRS feed

  • 问题

  • []里面的JSON没有key只有value

    Infor:{"tradeCode":"5040010","mess":"成功","data":[["801","玉璜","2114-10-01","订单","5.29","3.91","100%","100%","0.15%","0.0%","香港","香

    港","1.00","1","0","0","0.00","0.0","0.0","0.0"],["802","双龙玉璜","2114-10-01","订单","5.17","3.82","100%","100%","0.15%","0.0%","香港","香

    港","1.00","1","0","0","0.00","0.0","0.0","0.0"],["803","龙凤玉佩","2114-10-14","订单","2.32","1.72","100%","100%","0.3%","0.0%","香港","香

    港","0.01","1","0","0","0.00","0.0","0.0","0.0"],["805","古玉屏风","2114-11-11","订单","1.97","1.45","100%","100%","0.3%","0.0%","香港","香

    港","0.01","1","0","0","0.00","0.0","0.0","0.0"],["806","古玉玉龙","2114-12-18","订单","2.64","1.95","100%","100%","0.3%","0.0%","香港","香

    港","0.01","1","0","0","0.00","0.0","0.0","0.0"]],"sid":"M74W4OIDTICHQ9AUE3NLRVGG90KUHFHT","ver":"1.0.0","zip":"n","charset":"UTF-

    8","encrypt":"0","cryptKey":null,"digest":"","packetNo":1,"packetCounts":1,"returned":false}


    • 已编辑 Ansel.X 2015年1月15日 5:32
    2015年1月15日 5:17

答案

  • 你好:

    先定义一个对应的类型,然后通过JavaScriptSerializer类来进行反序列化就可以了,具体代码如下(已经测试,可以运行):

    using System.Web.Script.Serialization;
    
        class MyJsonObject
        {
            public string tradeCode { get; set; }
            public string mess { get; set; }
            public List<List<string>> data { get; set; }
            public string sid { get; set; }
            public string ver { get; set; }
            public string zip { get; set; }
            public string charset { get; set; }
            public string vencrypter { get; set; }
            public string cryptKey { get; set; }
            public string digest { get; set; }
            public int packetNo { get; set; }
            public int packetCounts { get; set; }
            public bool returned { get; set; }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                string json = File.ReadAllText("json.txt");
    
                JavaScriptSerializer oJS = new JavaScriptSerializer();
                MyJsonObject oRootObject = new MyJsonObject();
                oRootObject = oJS.Deserialize<MyJsonObject>(json);
    
            }
        }
    当然你也可以通过一些三方的json类库来完成这个操作,比如NewtonSoft.Json.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年1月16日 2:47
    版主