• Newtonsoft.Json序列化和反序列之javascriptConvert.SerializeObject,DeserializeObject,JsonWriter,JsonReader


    这里下载:http://www.newtonsoft.com/products/json/
    安装:
       1.解压下载文件,得到Newtonsoft.Json.dll
       2.在项目中添加引用..

    javascriptConvert.SerializeObject


     序列化和反序列在.net项目中: 

    Product product = new Product(); 
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
     
    string output = javascriptConvert.SerializeObject(product);
    //{
    //  "Name": "Apple",
    //  "Expiry": new Date(1230422400000),
    //  "Price": 3.99,
    //  "Sizes": [
    //    "Small",
    //    "Medium",
    //    "Large"
    //  ]
    //}

    javascriptConvert.DeserializeObject

    Product deserializedProduct = (Product)javascriptConvert.DeserializeObject(output, typeof(Product));

      
    读取JSON

    string jsonText = "['JSON!',1,true,{property:'value'}]"; 
    JsonReader reader = new JsonReader(new StringReader(jsonText)); 
    Console.WriteLine("TokenType		ValueType		Value"); 
    while (reader.Read())
    {
        Console.WriteLine(reader.TokenType + "		" + WriteValue(reader.ValueType) + "		" + WriteValue(reader.Value))
    }


    结果显示:

    TokenTypeValueTypeValue
    StartArray null null
    String System.String JSON!
    Integer System.Int32 1
    Boolean System.Boolean True
    StartObject null null
    PropertyName System.String property
    String System.String value
    EndObject null null
    EndArray null null

     

     

     

     

     

     

    JSON写入

    StringWriter sw = new StringWriter();

    JsonWriter writer = new JsonWriter(sw); 
    writer.WriteStartArray();
    writer.WriteValue("JSON!");
    writer.WriteValue(1);
    writer.WriteValue(true);
    writer.WriteStartObject();
    writer.WritePropertyName("property");
    writer.WriteValue("value");
    writer.WriteEndObject();
    writer.WriteEndArray(); 
    writer.Flush(); 
    string jsonText = sw.GetStringBuilder().ToString(); 
    Console.WriteLine(jsonText);
    // ['JSON!',1,true,{property:'value'}]


    这里会打印出: ['JSON!',1,true,{property:'value'}].

  • 相关阅读:
    kubernetes 之配置问题
    awk 输入文件包含某个关键字的整行记录或者某个字段
    Kubernetes资源在线生成工具
    使用print计算一串字符串各字符出现的个数
    Kubernetes 部署可视化dashboard管理面板
    nexus 3.37.302安装
    阿里云免费证书验证,nginx返回403,/.wellknown/pkivalidation/
    通过docker安装openldap并通过ldapadmin配置用户与第三方软件集成ldap配置
    kubernetes创建storageclass
    k8s快速生成一个deploymenet的yaml文件
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/5888321.html
Copyright © 2020-2023  润新知