zoukankan      html  css  js  c++  java
  • 问题:c# newtonsoft.json使用;结果:Newtonsoft.Json 用法

    Newtonsoft.Json 用法

    Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库.官方网站: 

    http://json.codeplex.com/
    使用方法
    1.首先下载你需要的版本,然后在应用程序中引用Newtonsoft.Json.dll 文件.
    2.引用命名空间using Newtonsoft.Json;  using Newtonsoft.Json.Linq;
    使用示例:
    string jsonText = "[{'a':'aaa','b':'bbb','c':'ccc'},{'a':'aa','b':'bb,'c':'cc'}]";  
    JArray ja =(JArray) JsonConvert.DeserializeObject(jsonText);  
    JObject o = (JObject)ja[1];  
    Console.WriteLine(o["a"]);  
    Console.WriteLine(ja[1]["a"]); 

    定义一个对象:vb
     string jsonText = "[{'a':'aaa','b':'bbb','c':'ccc'},{'a':'aa','b':'bb','c':'cc'}]";  
     List<vb> _list = JsonConvert.DeserializeObject<List<vb>>(jsonText);  
    Console.WriteLine(_list[1].a);  
    foreach (Customer c in _list)  
    {  
       Console.WriteLine(c.c);  
     }  
     
     

    c# 在.NET使用Newtonsoft.Json转换,读取,写入json

    https://www.ibm.com/developerworks/cn/web/wa-lo-json/ ,我在这里简单介绍下json:

        JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。和 XML 一样,JSON 也是基于纯文本的数据格式。由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,您可以用 JSON 传输一个简单的 String,Number,Boolean,也可以传输一个数组,或者一个复杂的 Object 对象。
         在.NET环境下面,我们使用Json.net来实现JSON数据的序列化和反序列化。
    http://sourceforge.net/projects/csjson/?source=dlp 下载JSON .NET插件和代码。
         然后在项目中进行引用Newtonsoft.Json.dll
         添加命名空间:using Newtonsoft.Json;
         下面介绍json序列化和反序列化的放个重要方法和例子:
    JsonConvert.SerializeObject(object value)序列化,它有个重载方法JsonConvert.SerializeObject(object value, params JsonConverter[] converters)。
    JsonConvert.DeserializeObject(string value, Type type),反序列化,它有个重载方法JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)
    这两个方法可以实现基本的序列化和反序列化要求,请看下面的例子:
      public class Person
        {
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private int age;
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
        }
     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Newtonsoft.Json;
     
    namespace JSONnet
    {
        public partial class test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Person person = new Person();
                person.Name = "GoldenEasy";
                person.Age = 25;
                string strSerializeJSON = JsonConvert.SerializeObject(person);
                Response.Write(strSerializeJSON);                     
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Newtonsoft.Json;
     
    namespace JSONnet
    {
        public partial class test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Person person = new Person();
                person.Name = "GoldenEasy";
                person.Age = 25;
                string strSerializeJSON = JsonConvert.SerializeObject(person);           
                Person user = (Person)JsonConvert.DeserializeObject(strSerializeJSON, typeof(Person));
                Response.Write(user.Name);
             
            }
        }
    }
    输出结果为:GoldenEasy
  • 相关阅读:
    用任务计划管理计划任务对付任务计划-禁止WPS提示升级
    破解激活Win10无风险?激活后删除激活工具无影响===http://www.pconline.com.cn/win10/693/6932077_all.html#content_page_4
    VS2013 密钥 – 所有版本
    2017面试题1
    模拟锚点
    输入框被软键盘遮
    资源(GitHub)
    全国城市部分js
    subline3 插件
    app下载——js设备判断
  • 原文地址:https://www.cnblogs.com/longphui/p/4773169.html
Copyright © 2011-2022 走看看