// CS.Json; net 4.0 var data = new JsonObject(); data.Add("cart_id", 123456); data.Add("item_count", 8); var items = new JsonArray(); for (int i = 0; i < 8; i++) { var node = new CS.Json.JsonObject(); node.Add("item_name", "项目名称"); node.Add("item_price", "价格"); items.Add(node); } data.Add("items", items); Response.Write(JsonConvert.SerializeObject(data)); //using Newtonsoft.Json; net 2.0 StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { JsonSerializer ser = new JsonSerializer(); jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("CPU"); jsonWriter.WriteValue("Intel"); jsonWriter.WritePropertyName("PSU"); jsonWriter.WriteValue("500W"); jsonWriter.WritePropertyName("Drives"); jsonWriter.WriteStartArray(); for (int i = 1; i <= 8; i++) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("num"); ser.Serialize(jsonWriter,i.ToString()); jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); jsonWriter.WriteStartArray(); jsonWriter.WriteValue("DVD read/writer"); jsonWriter.WriteComment("(broken)"); jsonWriter.WriteValue("500 gigabyte hard drive"); jsonWriter.WriteValue("200 gigabype hard drive"); jsonWriter.WriteEndArray(); jsonWriter.WriteEndObject(); } Response.Write(sb.ToString()); //using LitJson; net 2.0 //序列化 demo1 JsonData data = new JsonData(); data["name"] = "peiandsky"; data["age"] = 28; data["sex"] = "male"; var items = new JsonData(); for (int i = 0; i < 8; i++) { var node = new JsonData(); node["item_num"] = i.ToString(); items.Add(node); } data["items"] =items; string json1 = data.ToJson(); Response.Write(json1+ Environment.NewLine); //序列化 demo2 JsonWriter writer2 = new JsonWriter(); writer2.WriteObjectStart(); writer2.WritePropertyName("book"); writer2.WriteObjectStart(); writer2.WritePropertyName("title"); writer2.Write("android game!"); writer2.WritePropertyName("author"); writer2.Write("pei"); writer2.WritePropertyName("bookdetail"); writer2.WriteObjectStart(); writer2.WritePropertyName("pages"); writer2.Write(429); writer2.WritePropertyName("about"); writer2.Write(null); writer2.WriteObjectEnd(); writer2.WriteObjectEnd(); writer2.WritePropertyName("items"); writer2.WriteArrayStart(); for (int i = 1; i <= 8; i++) { writer2.WriteObjectStart(); writer2.WritePropertyName("item_num"); writer2.Write(i); writer2.WriteObjectEnd(); } writer2.WriteArrayEnd(); writer2.WriteObjectEnd(); Response.Write(writer2.ToString() + Environment.NewLine); //反序列化 demo JsonData pa = JsonMapper.ToObject(@"{""items"":[{""num"":""001""},{""num"":""002""}]}"); for (int i = 0; i < pa["items"].Count; i++) { Response.Write(pa["items"][i]["num"] + Environment.NewLine); }