[C#技术] .NET平台开源JSON库LitJSON的使用方法
一个简单示例:
//*** 将JsonData转换为JSON字符串 ***************************
String str2 = jd.ToJson();
下载地址 :下载
LitJSON是一个.NET平台下处理JSON格式数据的类库,小巧、快速。它的源代码使用C#编写,可以通过任何.Net平台上的语言进行调用,目前最新版本为LitJSON 0.5.0。
与以下这几个.Net平台上的开源JSON库相比,LitJSON的性能遥遥领先:
Jayrock version 0.9.8316
LitJSON version 0.3.0
Newtonsoft Json.NET version 1.1
下面介绍LitJSON中常用的方法:
以下示例需要先添加引用LitJson.dll,再导入命名空间 using LitJson;
点击直接下载LitJSON.dll,也可以到http://litjson.sourceforge.net去下载。
1、Json 与 C#中 实体对象 的相互转换
例 1.1:使用 JsonMapper 类实现数据的转换
ublic 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.87cool.com";
bill.Age = 3;
bill.Birthday = new DateTime(2007, 7, 17);
string json_bill = JsonMapper.ToJson(bill);
Console.WriteLine(json_bill);
//输出:{"Name":"www.87cool.com","Age":3,"Birthday":"07/17/2007 00:00:00"}
}
///
/// 将Json数据转换成实体类
///
public static void JsonToPerson()
{
string json = @"
{
""Name"" : ""www.87cool.com"",
""Age"" : 3,
""Birthday"" : ""07/17/2007 00:00:00""
}";
Person thomas = JsonMapper.ToObject(json);
Console.WriteLine("’87cool’ age: {0}", thomas.Age);
//输出:’87cool’ age: 3
}
}
例 1.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"}]
}
}
更详细的可参考 http://litjson.sourceforge.net/doc/manual.html (英文)
[C#技术] .NET平台开源JSON库LitJSON的使用方法的更多相关文章
- .NET平台开源JSON库LitJSON的使用方法
下载地址:LitJson.dll下载 一个简单示例: String str = "{'name':'cyf','id':10,'items':[{'itemid':1001,'itemnam ...
- (转).NET平台开源JSON库LitJSON的使用方法
一个简单示例: String str = "{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemi ...
- .NET平台开源JSON序列化
转载: http://blog.csdn.net/ddgweb/article/details/39643747 一个简单示例: String str = "{’name’:’cyf’,’i ...
- 开源 JSON 库解析性能对比( Jackson / Json.simple / Gson )
Json 已成为当前服务器与 web 应用之间数据传输的公认标准. 微服务及分布式架构经常会使用 Json 来传输此类文件,因为这已经是 webAPI 的事实标准. 不过正如许多我们习以为常的事情一样 ...
- 教程-delphi的开源json库:superobject,用法简介
困惑一天的问题 一个语句搞定了... 回头细说. superobject中的{$DEFINE UNICODE} 就是它,这是json官方推荐的Delphi处理json的包,地址: http://www ...
- fastjson是阿里巴巴的开源JSON解析库
fastjson的API十分简洁. String text = JSON.toJSONString(obj); //序列化 VO vo = JSON.parseObject("{...}&q ...
- 1. 初识Jackson -- 世界上最好的JSON库
要想人前显贵,必须背后受罪.关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis.中 ...
- 一个技术汪的开源梦 —— 基于 .Net Core 的公共组件之 Http 请求客户端
一个技术汪的开源梦 —— 目录 想必大家在项目开发的时候应该都在程序中调用过自己内部的接口或者使用过第三方提供的接口,咱今天不讨论 REST ,最常用的请求应该就是 GET 和 POST 了,那下面开 ...
- 爆料喽!!!开源日志库Logger的使用秘籍
日志对于开发来说是非常重要的,不管是调试数据查看.bug问题追踪定位.数据信息收集统计,日常工作运行维护等等,都大量的使用到.今天介绍著名开源日志库Logger的使用,库的地址:https://git ...
随机推荐
- ExtJs的事件机制Event(学员总结)
一.事件的三种绑定方式 1.HTML/DHTML 在标签中直接增加属性触发事件 [javascript] view plaincopy <script type="text/javas ...
- 创建DBLink语句
--linkName DBLink名 --username 用户名 --password 密码 --tns TNS配置字符串 create database link &linkName co ...
- Linux sed命令删除指定行
一.删除包含匹配字符串的行## 删除包含baidu.com的所有行sed -i '/baidu.com/d' domain.file 二.删除匹配行及后所有行## 删除匹配20160229的行及后面所 ...
- Linux2.6的所有内核版本
Index of /pub/linux/kernel/v2.6 Name Last modified Size Parent Directory - incr/ 03-Aug-2011 20:47 - ...
- win10开始菜单打不开的解决办法
解决方法: 1.在Win10系统下按Win+R打开运行,输入services.msc回车打开服务: 2.在服务中找到User Manager服务;3.打开usermanager服务属性,将其启动类型设 ...
- sql 汉字转首字母拼音
从网络上收刮了一些,以备后用 create function fun_getPY(@str nvarchar()) returns nvarchar() as begin declare @word ...
- jQuery1.6以上版本prop和attr的区别
- HTML&CSS基础学习笔记1.30-颜色的表达
颜色的表述 在网页中的颜色设置是非常重要,CSS的属性有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令颜色 ...
- C语言笔记(结构体与offsetof、container_of之前的关系)
关于结构体学习,需要了解:结构体的定义和使用.内存对齐.结构体指针.得到结构体元素的偏移量(offsetof宏实现) 一.复习结构体的基本定义和使用 typedef struct mystruct { ...
- 集合及特殊集合arrayList
1,运用集合 arrayList 首先复制Colections加 : 创建arrayList ar =new arrayList(); ArrayList具体提供的功能:属性 ...