Newtonsoft.Json 复杂结构类型的取值
2014-04-23 09:23
363 查看
由于项目中需要取网页上的数据,但是数据时JSON格式的,于是听同事介绍了一个Newtonsoft.Json.dll文件
具体操作如下:
我的字符串类型为:
需要循环取出Warehouse的值,这里我们使用了类构造函数
先定义所需要的类类型
然后就可以使用DecodeObject对象转换了
输出结果如下图
完整代码如下:
注:项目中的Newtonsoft是有3个版本的.NET2.0 、3.5、 4.0 要引用对应的版本才能找到名称空间;
Utility.cs完整代码如下:
具体操作如下:
我的字符串类型为:
先定义所需要的类类型
string str = "{ 'root':{ 'items': [{'Warehouse': '219410', 'City_St': 'Morrow, GA', 'Bales': '264'},{'Warehouse': '240124', 'City_St': 'Climax, GA', 'Bales': '88'},{'Warehouse': '266405', 'City_St': 'Funston, GA', 'Bales': '176'},{'Warehouse': '297005', 'City_St': 'Meigs, GA', 'Bales': '264'},{'Warehouse': '710527', 'City_St': 'Greenville, SC', 'Bales': '440'}] }} "; Root T = Utility.JsonUtility.DecodeObject<Root>(str); for (int i = 0; i < T.root.items.Count; i++) { Response.Write(T.root.items[i].Warehouse+"\n"); }
输出结果如下图
完整代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Newtonsoft.Json; 8 using Newtonsoft.Json.Converters; 9 using System.IO; 10 11 public partial class _Default : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 string str = "{ 'root':{ 'items': [{'Warehouse': '219410', 'City_St': 'Morrow, GA', 'Bales': '264'},{'Warehouse': '240124', 'City_St': 'Climax, GA', 'Bales': '88'},{'Warehouse': '266405', 'City_St': 'Funston, GA', 'Bales': '176'},{'Warehouse': '297005', 'City_St': 'Meigs, GA', 'Bales': '264'},{'Warehouse': '710527', 'City_St': 'Greenville, SC', 'Bales': '440'}] }} "; 16 Root T = Utility.JsonUtility.DecodeObject<Root>(str); 17 for (int i = 0; i < T.root.items.Count; i++) 18 { 19 Response.Write(T.root.items[i].Warehouse+"\n"); 20 } 21 } 22 23 public class MyList 24 { 25 public string Warehouse { get; set; } 26 public string City_St { get; set; } 27 } 28 public class Items 29 { 30 public IList<MyList> items { get; set; } 31 } 32 public class Root 33 { 34 public Items root { get; set; } 35 } 36 37 private static string StringFormat(string str, Type type) 38 { 39 if (type == typeof(string)) 40 { 41 str = String2Json(str); 42 str = "\"" + str + "\""; 43 } 44 else if (type == typeof(DateTime)) 45 { 46 str = "\"" + str + "\""; 47 } 48 else if (type == typeof(bool)) 49 { 50 str = str.ToLower(); 51 } 52 return str; 53 } 54 55 }
注:项目中的Newtonsoft是有3个版本的.NET2.0 、3.5、 4.0 要引用对应的版本才能找到名称空间;
Utility.cs完整代码如下:
相关文章推荐
- Newtonsoft.Json 复杂结构类型的取值
- XML多层复杂结构转Dictionary<String,Object>类型
- C# 使用Newtonsoft.Json序列化自定义类型
- Newtonsoft.Json 处理多态类型的反序列化
- Newtonsoft.Json序列化DateTime类型数据为字符串时候,带了一个T
- 坑:golang按照map解析json结构时,int类型会被升级为float64
- fastjson处理复杂类型转换以及特性研究
- 使用Gson轻松解决复杂结构的Json数据解析
- JSON复杂数据处理之Json树形结构数据转Java对象并存储到数据库的实现
- C# WebService的简单和复杂参数类型和结果的JSON格式
- Retrofit 传递json 和 复杂参数类型List<T>
- Newtonsoft.Json利用IsoDateTimeConverter处理日期类型
- Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
- 如何用Gson解析复杂结构的json数据
- 黑马程序员——c语言的复杂数据类型:指针和结构体
- fetch post application/json 传递复杂类型
- 复杂的变量类型:枚举,结构,数组
- Newtonsoft.Json利用IsoDateTimeConverter处理日期类型
- 使用fastjson的filter机制将String属性转成复杂类型
- json字符串转为map结构,复杂json字符串转为map结构