JSON

常伟华(Changweihua) 的个人站,共享最新.NET技术心得

字号+ 作者:H5之家 来源:H5之家 2015-10-06 16:08 我要评论( )

C#中使用LitJson - 常伟华(Changweihua) 的个人站,共享最新.NET技术心得,探讨最新PC和移动端开发技术,共享开发中遇到的技术难题和解决方法

1、使用 JsonMapper 类实现数据的转换
public class Person { public string Name { get; set; } public int Age { get; set; } public DateTime Birthday { get; set; } } public class JsonSample { public static void Main() { PersonToJson(); JsonToPerson(); } /// /// 将实体类转换成Json格式 /// public static void PersonToJson() { Person bill = new Person(); bill.Name = "www.cmono .net">cmono .net"; bill.Age = 3; bill.Birthday = new DateTime(2007, 7, 17); string json_bill = JsonMapper.ToJson(bill); Console.WriteLine(json_bill); //输出:{"Name":"www.cmono .net">cmono .net","Age":3,"Birthday":"07/17/2007 00:00:00"} } /// /// 将Json数据转换成实体类 /// public static void JsonToPerson() { string json = @" { ""Name"" : ""www.cmono .net">cmono .net"", ""Age"" : 3, ""Birthday"" : ""07/17/2007 00:00:00"" }"; Person thomas = JsonMapper.ToObject(json); Console.WriteLine("'87cool' age: {0}", thomas.Age); //输出:'87cool' age: 3 } }2、使用 JsonMapper 类将Json字符串转换为C#认识的JsonData,再通过Json数据的属性名或索引获取其值。

在C#中读取JsonData对象 和 在JavaScript中读取Json对像的方法完全一样;

对Json的这种读取方式在C#中用起来非常爽,同时也很实用,因为现在很多网络应用提供的API所返回的数据都是Json格式的,

如Flickr相册API返回的就是json格式的数据。

 

public static void LoadAlbumData(string json_text) { JsonData data = JsonMapper.ToObject(json_text); Console.WriteLine("Album's name: {0}", data["album"]["name"]); string artist = (string)data["album"]["name"]; int year = (int)data["album"]["year"]; Console.WriteLine("First track: {0}", data["album"]["tracks"][0]); }

2、C# 中对 Json 的 Readers 和 Writers

 

例 2.1:JsonReader类的使用方法

 

public class DataReader { public static void Main () { string sample = @"{ ""name"" : ""Bill"", ""age"" : 32, ""awake"" : true, ""n"" : 1994.0226, ""note"" : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ] }"; ReadJson (sample); } //输出所有Json数据的类型和值 public static void ReadJson (string json) { JsonReader reader = new JsonReader (json); Console.WriteLine ("{0,14} {1,10} {2,16}", "Token", "Value", "Type"); Console.WriteLine (new String ('-', 42)); while (reader.Read()) { string type = reader.Value != null ? reader.Value.GetType().ToString() : ""; Console.WriteLine("{0,14} {1,10} {2,16}", reader.Token, reader.Value, type); } } } //输出结果: // Json类型 值 C#类型 //------------------------------------------ // ObjectStart // PropertyName name System.String // String Bill System.String // PropertyName age System.String // Int 32 System.Int32 // PropertyName awake System.String // Boolean True System.Boolean // PropertyName n System.String // Double 1994.0226 System.Double // PropertyName note System.String // ArrayStart // String life System.String // String is System.String // String but System.String // String a System.String // String dream System.String // ArrayEnd // ObjectEnd

例 2.2:JsonWriter类的使用方法

 

public class DataReader { //通过JsonWriter类创建一个Json对象 public static void WriteJson () { System.Text.StringBuilder sb = new System.Text.StringBuilder(); JsonWriter writer = new JsonWriter (sb); writer.WriteArrayStart (); writer.Write (1); writer.Write (2); writer.Write (3); writer.WriteObjectStart (); writer.WritePropertyName ("color"); writer.Write ("blue"); writer.WriteObjectEnd (); writer.WriteArrayEnd (); Console.WriteLine (sb.ToString ()); //输出:[1,2,3,{"color":"blue"}] } }

标签:


《C#中使用LitJson》 由 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。

相邻依据:发表时间

《只有mdf文件和ldf文件,恢复数据库》

《解决ie9 不支持extjs3.3对象的 “createContextualFragment”属性或方法》

  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5
  • 0 条评论 / 点击此处发表评论

    Tab Content 5

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • 自定义jinja2 过滤器

      自定义jinja2 过滤器

      2016-02-14 10:00

    • ASP.NET Web API 2 返回 Json格式

      ASP.NET Web API 2 返回 Json格式

      2016-02-10 17:18

    • ExtAspNet应用技巧(二十二) - Ext4JSLint之JSON文件创建树控件(2)

      ExtAspNet应用技巧(二十二) - Ext4JSLint之JSON文件创建树控件(2)

      2016-01-25 09:02

    • newtonsoft.json 6.0(20141020)

      newtonsoft.json 6.0(20141020)

      2016-01-18 16:08

    网友点评
    8