zoukankan      html  css  js  c++  java
  • C# 中Newtonsoft.Json的安装和使用

    官网参考:http://json.codeplex.com/

    在程序包管理控制台,键入NuGet命令  install-package Newtonsoft.Json  安装Newtonsoft.Json

    PM> install-package Newtonsoft.Json
    正在安装“Newtonsoft.Json 6.0.5”。
    已成功安装“Newtonsoft.Json 6.0.5”。
    正在将“Newtonsoft.Json 6.0.5”添加到 MVCDemo.Model。
    已成功将“Newtonsoft.Json 6.0.5”添加到 MVCDemo.Model。

    原文:The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping the .NET object property names to the JSON property names and copies the values for you.

    译文:最快的JSON和文本之间的转换方法。NET对象是使用jsonserializer。该jsonserializer转换成JSON等效.NET对象  通过.NET对象属性名称映射成JSON属性名称,并且给你属性对应的Value值。(翻译水平太差,勿喷)

    Collapse imageJsonConvert

    原文:For simple scenarios where you want to convert to and from a JSON string the SerializeObject() and DeserializeObject() methods on JsonConvert provide an easy to use wrapper over JsonSerializer.

    翻译:对于简单的情况下,包装在jsonserialize中的JsonConvert,他的serializeobject()和deserializeobject()方法,让对象和Json字符串之间的转换变的更加简单(翻译水平太差,勿喷)

    用法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Product product = new Product();
     
    product.Name = "Apple";
    product.ExpiryDate = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
     
     8string output = JsonConvert.SerializeObject(product);
    //{
    //  "Name": "Apple",
    //  "ExpiryDate": "2008-12-28T00:00:00",
    //  "Price": 3.99,
    //  "Sizes": [
    //    "Small",
    //    "Medium",
    //    "Large"
    //  ]
    //}
     
    Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

     原文: SerializeObject and DeserializeObject both have overloads that take a JsonSerializerSettings object. JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.

    译文:SerializeObject和DeserializeObject 都有带有JsonSerializerSettings 对象的重载方法,JsonSerializerSettings 让你使用下面列出的许多jsonserializer设置时仍然使用简单的序列化方法。(翻译水平太差,勿喷)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //转换成Json字符串形式,返回格式:[{...},{...},{...}]
          public static  string ToJson(this object value, string defaultValue = """") {
              if (value==null) {
                  return defaultValue;
              }
              else {
                  var json= JsonConvert.SerializeObject(value);//序列化
                  return json.Equals("null") ? defaultValue : json;
              }
          }
  • 相关阅读:
    解决 搭建Jekins过程中 启动Tomcat的java.net.UnknownHostException异常
    射手和农场主
    java 和 JS(javaScript)中的反斜杠正则转义
    分享修改密码的SharePoint Web part: ITaCS Change Password web part
    分享微软官方Demo用的SharePoint 2010, Exchange 2010, Lync 2010虚拟机
    Office 365 的公共网站的一些限制及解决的办法
    SharePoint 2013 关闭 customErrors
    安装 KB2844286 导致SharePoint 2010 XSLT web part 显示出现错误
    安装Office Web Apps Server 2013 – KB2592525安装失败
    如何将hyper-v虚拟机转换成vmware的虚拟机- 转换SharePoint 2010 Information Worker Demonstration and Evaluation Virtual Machine (SP1)
  • 原文地址:https://www.cnblogs.com/qqhewei/p/10708866.html
Copyright © 2011-2022 走看看