zoukankan      html  css  js  c++  java
  • JSON.NET 简单的使用

    JSON.NET(http://json.codeplex.com/)使用来将.NET中的对象转换为JSON字符串(序列化?),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?)

    首先为了例子随便定义一个类型:

    复制代码
    public class Product
    {
        public string Name { get; set; }
        public DateTime Expiry { get; set; }
        public decimal Price { get; set; }
        public string[] Sizes { get; set; }
    
        public override string ToString()
        {
            return string.Format("Name:{0},Expiry:{1},Price:{2},SizesCount:{3}"
                , Name, Expiry, Price, Sizes.Length);
        }
    }
    复制代码

    初始化对象:

    复制代码
    public static void Main(string[] passwordargs)
    {
        Product product = new Product()
        {
            Name = "android",
            Expiry = DateTime.Now,
            Price = 2000,
            Sizes = new string[] { "1.5", "2.2", "4.1" }
        };
    }
    复制代码

    进行到JSON的转换:

    Console.WriteLine(JsonConvert.SerializeObject(product));

    输出结果:

    {"Name":"android","Expiry":"2013-08-30T09:50:11.5147845+08:00","Price":2000.0,"Sizes":["1.5","2.2","4.1"]}

    其它看起来一切正常,除了这个日期有点怪

    格式化日期:

    //设置日期时间的格式,与DataTime类型的ToString格式相同
    IsoDateTimeConverter iso = new IsoDateTimeConverter();
    iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    
    Console.WriteLine(JsonConvert.SerializeObject(product, iso));

    输出结果:

    {"Name":"android","Expiry":"2013-08-30 09:53:58","Price":2000.0,"Sizes":["1.5","2.2","4.1"]}

    从JSON到对象的转换:

    string str = "{"Name":"android","Expiry":"2013-08-30 09:53:58","Price":2000.0,"Sizes":["1.5","2.2","4.1"]}";
    
    Product p = (Product)JsonConvert.DeserializeObject(str, typeof(Product));
    
    Console.WriteLine(p.ToString());

    输出结果:

    Name:android,Expiry:2013/8/30 9:53:58,Price:2000.0,SizesCount:3

    从JSON到键值对的转换:

    复制代码
    string strJson = @"{""Name1"": ""小明"",""Name2"": ""小花"",""Name3"": ""小红""}";
    
    Dictionary<string, string> _dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(strJson);
    
    foreach (KeyValuePair<string, string> kp in _dictionary)
    {
        Console.WriteLine(kp.Key + ":" + kp.Value);
    }
    复制代码

    输出结果:

    Name1:小明 
    Name2:小花 
    Name3:小红

    从字符串转换到JSON对象,以及JSON对象的简单使用:

    复制代码
    string strJson2 = @"{ ""student"": { ""Name1"": ""小明"" , ""Name2"": ""小花"" , ""Name3"": ""小红""} }";
    
    JObject jsonObj = JObject.Parse(strJson2);
    
    Console.WriteLine(jsonObj["student"]["Name1"].ToString());
    Console.WriteLine(jsonObj["student"]["Name2"].ToString());
    Console.WriteLine(jsonObj["student"]["Name3"].ToString());
    复制代码

    输出结果:

    小明 
    小花 
    小红

    直接生成JSON对象:

    复制代码
    JObject json =
        new JObject(
        new JProperty("Channel",
            new JObject(
            new JProperty("title", "JSON"),
            new JProperty("link", "JSON.NET"),
            new JProperty("description", "JSON.NET Description"),
            new JProperty("items",
                new JArray(
                new JObject(new JProperty("haha1", "123")),
                new JObject(new JProperty("haha2", "456")),
                new JObject(new JProperty("haha3", "789"))
                    )))));
    
    Console.WriteLine(json.ToString());
    复制代码

    输出结果:


      "Channel": { 
        "title": "JSON", 
        "link": "JSON.NET", 
        "description": "JSON.NET Description", 
        "items": [ 
          { 
            "haha1": "123" 
          }, 
          { 
            "haha2": "456" 
          }, 
          { 
            "haha3": "789" 
          } 
        ] 
      } 
    }

    暂时先记录这么多,以后再继续补充

  • 相关阅读:
    添加Google Admob到ANDROID应用中
    linux命令及实例说明一:cd、ls、rmdir、rm、mkdir
    android中dip、dp、px、sp和屏幕密度
    在程序中读取ANDROID应用的程序名称和版本号
    linux常用命令及实例二:cp、mv、chown、chmod、find
    Android开发,常用的终端命令
    eclipse里配置solr开发测试环境
    自定义Dialog之Progress(二)
    android 判断网络状态
    hive cli命令行选项
  • 原文地址:https://www.cnblogs.com/PearlRan/p/4833040.html
Copyright © 2011-2022 走看看