.NET平台开源JSON库LitJSON的使用方法
String str = "{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemid’:1002,’itemname’:’hello2’}]}";
//*** 读取JSON字符串中的数据 *******************************
JsonData jd = JsonMapper.ToObject(str);
String name = (String)jd["name"];
long id = (long)jd["id"];
JsonData jdItems = jd["items"];
int itemCnt = jdItems.Count;
// 数组 items 中项的数量
foreach (JsonData item in jdItems)
// 遍历数组 items
{int itemID = (int)item["itemid"];
String itemName = (String)item["itemname"];
}
//*** 将JsonData转换为JSON字符串 ***************************
String str2 = jd.ToJson();
与以下这几个.Net平台上的开源JSON库相比,LitJSON的性能遥遥领先:
Jayrock version 0.9.8316
LitJSON version 0.3.0
Newtonsoft Json.NET version 1.1
下面介绍LitJSON中常用的方法:
以下示例需要先添加引用LitJson.dll,再导入命名空间 using LitJson;
1、Json 与 C#中 实体对象 的相互转换
例 1.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.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
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"}]
}
}
.NET平台开源JSON库LitJSON的使用方法的更多相关文章
- [C#技术] .NET平台开源JSON库LitJSON的使用方法
一个简单示例: String str = "{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemi ...
- (转).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 ...
- 高性能JSON库---FastJson(阿里巴巴)
1.FastJSON简单介绍 Fastjson是一个Java语言编写的高性能功能完好的JSON库. 它採用一种"假定有序高速匹配"的算法,把JSON Parse的性能提升到极致,是 ...
- SNF快速开发平台MVC-EasyUI3.9之-WebApi和MVC-controller层接收的json字符串的取值方法和调用后台服务方法
最近项目组很多人问我,从前台页面传到后台controller控制层或者WebApi 时如何取值和运算操作. 今天就都大家一个在框架内一个取值技巧 前台JS调用代码: 1.下面是选中一行数据后右键点击时 ...
- 28 个 C/C++ 开源 JSON 程序库性能及标准符合程度评测
28 个 C/C++ 开源 JSON 程序库性能及标准符合程度评测 坊间有非常多的 C/C++ JSON 库,怎么选择是一个难题. [nativejson-benchmark](https://git ...
随机推荐
- Android笔记之网络状态推断
1.首先当然得在 manifest 中加入检查网络状态的权限: <uses-permission android:name="android.permission.ACCESS_NET ...
- Swift学习笔记(二十二)——字典的基本操作
(1)计算字典长度 . (2)推断字典是否为空 . (3)查询字典元素 . (4)取出字典元素进行拼接 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/fo ...
- 0x54 树形DP
树形DP我只知道千万别写森林转二叉树慢的要死 没有上司的舞会 水!裸! #include<cstdio> #include<cstring> #include<cstdl ...
- 从有约束条件下的凸优化角度思考神经网络训练过程中的L2正则化
从有约束条件下的凸优化角度思考神经网络训练过程中的L2正则化 神经网络在训练过程中,为应对过拟合问题,可以采用正则化方法(regularization),一种常用的正则化方法是L2正则化. 神经网络中 ...
- 如何用jQuery实现div随鼠标移动而移动?(详解)----2017-05-12
重点是弄清楚如何获取鼠标现位置与移动后位置,div现在位置与移动后位置: 用jQuery实现div随鼠标移动而移动,不是鼠标自身的位置!!而是div相对于之前位置的移动 代码如下:(注意看绿色部分的解 ...
- 【media-queries】媒体查询,为了响应式设计而生
目录 简介 语法 常用尺寸 一 简介 针对现在纷杂的设备,css3中加入,可以查询你的浏览类型(screen彩色屏幕, print, all)和css属性判断. 最常用的就是查询屏幕大小,给予适合的展 ...
- 打开word2010每次都要配置进度的解决办法
作者:朱金灿 来源:http://blog.csdn.net/clever101 不小心把ms office2010搞坏了,于是重装ms office2010,结果一打开word文档时总是出现下面的对 ...
- ContentProvider 的使用
1.简单示例:通过ContentProvider暴露数据库,然后读取数据. 2.先加上一个工具类,用来使用copy assets下面的db文件代码如下: public class MyDBOpenHe ...
- 基于MapReduce的贝叶斯网络算法研究参考文献
原文链接(系列):http://blog.csdn.net/XuanZuoNuo/article/details/10472219 论文: 加速贝叶斯网络:Accelerating Bayesian ...
- CSS读书笔记(3)---清除浮动的几种方法
浮动元素容易造成页面错位现象.下面说说关于清除浮动的几种方法. 首先.先创建一个浮动导致错位的页面. <!DOCTYPE html> <html lang="en" ...