JSON,JSON多级数组.NET接收转成字符串DataTable或者DataSet

我是个特别懒的人啊,也是个小渣渣,但以后得养成写博客得习惯了,好了,废话不多说,直接上代码,中心思想自己领会啊。
json 串样例:
JSON,JSON多级数组.NET接收转成字符串DataTable或者DataSet_第1张图片

 {"FAG": "1",
 "RET": 
 {"Air_Data":[
  {
    "AIRLINE": "CA",
    "FULL_PRICE": "1280",
    "SHARE_FLIGHT_NO": "",
    "Seats": [
      {
        "SEAT": "S1",
        "SEAT_GRADE": "Y",
        "FUEL_PRICE": "0",
        "SIGN_REFOUND": {
          "SIGN": "测试"
        },
        "SEATS_COUNT": "A"
      },
      {
        "SEAT": "S1",
        "SEAT_GRADE": "Y",
        "FUEL_PRICE": "0",
        "SIGN_REFOUND": {
          "SIGN": "测试"
        },
        "SEATS_COUNT": "8"
      }
    ]
  }
],"Code":"200"},"Error": ""}

这种json串有多层关系,所以在建立实体类得时候,最简单得方式就是按照这个json格式去做,字符串,List集合等。

实体样例:(不明白得仔细对比上面得json数组,从最里面得小对象往最外层对比)

public class TestClass
    {
        [Serializable]
        public class SIGN_REFOUND
        {
            public string SIGN { get; set; }
        }
    [Serializable]
    public class SeatsItem
    {
        public string SEAT { get; set; }
        public string SELL_PRICE { get; set; }
        public string SIGN_REFOUND { get; set; }
        public string FUEL_PRICE { get; set; }
        public SIGN_REFOUND SIGN_REFOUND { get; set; }
        public string SEATS_COUNT { get; set; }
    }

    [Serializable]
    public class Air_DataItem
    {
        public string AIRLINE { get; set; }
        public string FULL_PRICE { get; set; }
        public string SHARE_FLIGHT_NO { get; set; }
        public List Seats { get; set; }
    }

    [Serializable]
    public class RET
    {
        public List Air_Data { get; set; }
        public string Code { get; set; }
        public List Seats { get; set; }
    }

    [Serializable]
    public class Root
    {
        public string FAG { get; set; }
        public RET RET { get; set; }
        public string Error { get; set; }
        public List Air_Data { get; set; }
        public List Seats { get; set; }
    }
}

方法样例:
关键两句代码,反序列化

 System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
             newExWebService.Model.TestClass.Root jsonAir_DataItem = js.Deserialize(jsonStr);

测试代码

        [WebMethod(Description = "test")]
        public string test(String jsonStr)
        {
             System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
             newExWebService.Model.TestClass.Root jsonAir_DataItem = js.Deserialize(jsonStr);
             List airlist = jsonAir_DataItem.RET.Air_Data;
             String resultStr = "";
             int flag =0;
             //第一层
             String FAG = jsonAir_DataItem.FAG;
             Object RET = jsonAir_DataItem.RET;
            //第二层
            foreach (newExWebService.Model.TestClass.Air_DataItem item in airlist)
            {
                String AIRLINE = item.AIRLINE;
                String FULL_PRICE = item.FULL_PRICE;
                String SHARE_FLIGHT_NO = item.SHARE_FLIGHT_NO;
                //第三层
                List SeatsItemlist = item.Seats;
                foreach (newExWebService.Model.TestClass.SeatsItem seatitem in SeatsItemlist)
                {
                    String SEAT = seatitem.SEAT;
                    String SEAT_CN = seatitem.SELL_PRICE;
                    String FUEL_PRICE = seatitem.FUEL_PRICE;
                   //第四层
                    String SIGN = seatitem.SIGN_REFOUND.SIGN;
                    flag++;
                    resultStr += "第" + flag + "条:" + "SEAT:" + SEAT + "SEAT_CN:" + SEAT_CN + SIGN +  "\r\n";
               
                }
            }
            return "共计" + flag + "条数据," + resultStr;
     }

到这里,json得数据已经都可以解析出来了,其他你想做什么操作都可以,转成DataTable或者DataSet,大概就是new DataTable();然后创建DataRow循环往DataTable里面填充值就好了。

												--Author:小北

你可能感兴趣的:(.NET,C#)