//using Newtonsoft.Json; StringBuilder sb = new StringBuilder(); string str = "[{ID:'1',Name:'zhangsan',Other:[{Age:'111'}]}]"; JavaScriptArray javascript = (JavaScriptArray)JavaScriptConvert.DeserializeObject(str); if (javascript.Count > 0) { JavaScriptObject obj = (JavaScriptObject)javascript[0]; sb.Append("ID:" + obj["ID"].ToString() + "<br/>"); sb.Append("Name:" + obj["Name"].ToString() + "<br/>"); JavaScriptArray json = (JavaScriptArray)obj["Other"]; if (json.Count > 0) { JavaScriptObject jsonobj = (JavaScriptObject)json[0]; sb.Append("Age:" + jsonobj["Age"].ToString()); } } Response.Write(sb.ToString());
//有時候版本不同上面的有錯誤,,可以試試這種 StringBuilder sb = new StringBuilder(); protected void Page_Load(object sender, EventArgs e) { string jsontext = "[{ID:'1',Name:'zhangsan',Other:[{Age:'111'}]}]"; Newtonsoft.Json.Linq.JArray ja = (Newtonsoft.Json.Linq.JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(jsontext); foreach (Newtonsoft.Json.Linq.JObject item in ja) { string aa = item["Name"].ToString(); Newtonsoft.Json.Linq.JArray ja0 = (Newtonsoft.Json.Linq.JArray)item["Other"]; foreach (Newtonsoft.Json.Linq.JObject item0 in ja0) { string bb = item0["Age"].ToString(); } }
StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果報錯則使用JsonWriter writer = new JsonTextWriter(sw); writer.WriteStartArray(); writer.WriteValue("JSON!"); writer.WriteValue(1); writer.WriteValue(true); writer.WriteStartObject(); writer.WritePropertyName("property"); writer.WriteValue("value"); writer.WriteEndObject(); writer.WriteStartObject(); writer.WritePropertyName("data"); writer.WriteValue(_str); writer.WriteEndObject(); writer.WriteEndArray(); writer.Flush(); string jsonText = sw.GetStringBuilder().ToString(); Response.Write(jsonText);
|
|