java newtonsoft.json_.NET(C#) Json.Net(newtonsoft)操作处理(解析)JSON数据(LINQ to JSON)

1、使用LINQ for JSONJObject o = JObject.Parse(@"{

'CPU': 'Intel',

'Drives': [

'DVD read/writer',

'500 gigabyte hard drive'

]

}");

string cpu = (string)o["CPU"];

// Intel

string firstDrive = (string)o["Drives"][0];

// DVD read/writer

IList allDrives = o["Drives"].Select(t => (string)t).ToList();

// DVD read/writer

// 500 gigabyte hard drive

2、解析JSON

1)解析JSON文本

可以使用Parse(string)从字符串读取JSON值。string json = @"{

CPU: 'Intel',

Drives: [

'DVD read/writer',

'500 gigabyte hard drive'

]

}";

JObject o = JObject.Parse(json);

string json = @"[

'Small',

'Medium',

'Large'

]";

JArray a = JArray.Parse(json);

2)从文件加载JSONusing (StreamReader reader = File.OpenText(@"c:\person.json"))

{

JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(reader));

// do stuff

}

3、创建JSON

1)手动创建JSON

一次设置一个值并创建一个对象和数组可以让您完全控制,但是它比其他选项更冗长。JArray array = new JArray();

JValue text = new JValue("Manual text");

JValue date = new JValue(new DateTime(2000, 5, 23));

array.Add(text);

array.Add(date);

string json = array.ToString();

// [

// "Manual text",

// "2000-05-23T00:00:00"

// ]

2)用LINQ创建JSON

使用LINQ声明性地创建JSON对象是一种从值集合创建JSON的快速方法。List posts = GetPosts();

JObject rss =

new JObject(

new JProperty("channel",

new JObject(

new JProperty("title", "James Newton-King"),

new JProperty("link", "http://james.newtonking.com"),

new JProperty("description", "James Newton-King's blog."),

new JProperty("item",

new JArray(

from p in posts

orderby p.Title

select new JObject(new JProperty("title", p.Title),

new JProperty("description", p.Description),

new JProperty("link", p.Link),

new JProperty("category",

new JArray(

from c in p.Categories

select new JValue(c)))))))));

Console.WriteLine(rss.ToString());

//{

// "channel": {

// "title": "James Newton-King",

// "link": "http://james.newtonking.com",

// "description": "James Newton-King\'s blog.",

// "item": [

// {

// "title": "Json.NET 1.3 + New license + Now on CodePlex",

// "description": "Announcing the release of Json.NET 1.3, the MIT license and being available on CodePlex",

// "link": "http://james.newtonking.com/projects/json-net.aspx",

// "category": [

// "Json.NET",

// "CodePlex"

// ]

// },

// {

// "title": "LINQ to JSON beta",

// "description": "Announcing LINQ to JSON",

// "link": "http://james.newtonking.com/projects/json-net.aspx",

// "category": [

// "Json.NET",

// "LINQ"

// ]

// }

// ]

// }

//}

3)通过object创建JSON

最后一个选项是使用FromObject()方法从非JSON类型创建JSON对象。在内部,FromObject将使用JsonSerializer将对象序列化为LINQ到JSON对象,而不是文本。

下面的示例展示了如何从匿名对象创建JSON对象,但是任何.net类型都可以与FromObject一起用于创建JSON。JObject o = JObject.FromObject(new

{

channel = new

{

title = "James Newton-King",

link = "http://james.newtonking.com",

description = "James Newton-King's blog.",

item =

from p in posts

orderby p.Title

select new

{

title = p.Title,

description = p.Description,

link = p.Link,

category = p.Categories

}

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值