• c# json使用集


                // 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);
    
                }
    
  • 相关阅读:
    jquery操作select(取值,设置选中)
    jQuery懒加载插件 – jquery.lazyload.js简单调用
    js获取URL中的参数
    数据结构之队列C++版
    数据结构之堆栈java版
    数据结构之堆栈C++版
    c++操作符重载
    QT状态机
    c++/c关于函数指针
    学习Qt的一点小感想
  • 原文地址:https://www.cnblogs.com/hack0573/p/4418028.html
Copyright © 2020-2023  润新知