json转对象除了Newtonsoft.Json,还有net自带的System.Web.Extensions下的System.Web.Script.Serialization
string jsonText = "{'name':'test','phone':'18888888888','image':[{'name':'img1','data':'data1'},{'name':'img2','data':'data2'},{'name':'img3','data':'data3'}]}"; JavaScriptSerializer js = new JavaScriptSerializer(); List<string> list = js.Deserialize<List<string>>(jsonText); JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); //string s = js.Serialize(object); JArray jar = JArray.Parse(jo["image"].ToString()); MessageBox.Show(jar[1].ToString()); //MessageBox.Show(jo.ToString()); string zone = jo["name"].ToString(); string zone_en = jo["phone"].ToString(); //JArray jar = JArray.Parse(jo["image"].ToString()); //MessageBox.Show(zone); //MessageBox.Show(zone_en); for (var i = 0; i < jar.Count; i++) { JObject j = JObject.Parse(jar[i].ToString()); //MessageBox.Show(j["name"].ToString()); //MessageBox.Show(j["data"].ToString()); }
对象转数组
string jsonText = "{'name':'test','phone':'18888888888','image':[{'name':'img1','data':'data1','v':12},{'name':'img2','data':'data2','v':12},{'name':'img3','data':'data3','v':12}]}"; JavaScriptSerializer js = new JavaScriptSerializer(); List<string> list = js.Deserialize<List<string>>(jsonText); JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); //string s = js.Serialize(object); JArray jar = JArray.Parse(jo["image"].ToString()); //MessageBox.Show(jar[1].ToString()); //MessageBox.Show(jo.ToString()); string zone = jo["name"].ToString(); string zone_en = jo["phone"].ToString(); //JArray jar = JArray.Parse(jo["image"].ToString()); //MessageBox.Show(zone); //MessageBox.Show(zone_en); List<object> jjw = new List<object>(); for (var i = 0; i < jar.Count; i++) { JObject j = JObject.Parse(jar[i].ToString()); List<object> jj = new List<object>(); jj.Add(j["name"].ToString()); jj.Add(j["data"].ToString()); jj.Add(j["v"]); jjw.Add(JsonHelper.ToJson(jj)); //MessageBox.Show(j["name"].ToString()); //MessageBox.Show(j["data"].ToString()); } MessageBox.Show(JsonHelper.ToJson(jjw));