1. 程式人生 > 實用技巧 >Json陣列,Newtonsoft.Json應用

Json陣列,Newtonsoft.Json應用

Json越來越流行,而Newtonsoft.Json是處理Json的強大工具,客戶端返回的資料為

{"Data":
[{"out_put":"01","hosting":"Z01_HOS_253_M1","value":185},
{"out_put":"02","hosting":"Z01_HOS_253_M1","value":0},
{"out_put":"01","hosting":"Z01_HOS_256_M1","value":1662},
{"out_put":"02","hosting":"Z01_HOS_256_M1","value":29},
{"out_put":"01","hosting":"Z01_HOS_251_M1","value":3646}
]}

建立類Tr_Lift_Big,表示每行記錄

     class Tr_Lift_Big
    {
          public string out_put { get; set;}
          public string hosting { get; set; }
        public int value { get; set; }
    }

處理每個物件的程式碼

    JObject jsonObj;
    jsonObj = JObject.Parse("{\"Data\":。。。6}]}");
    JArray jsonArray = JArray.Parse(jsonObj["
Data"].ToString()); List<Tr_Lift_Big> liftList = new List<Tr_Lift_Big>(); liftList = JsonConvert.DeserializeObject<List<Tr_Lift_Big>>(jsonArray.ToString()); int mValue; for (int i = 0; i < liftList.Count; ++i) { mValue = liftList[i].value; }