一些反序列化JSON与JSON.NET [英] Deserializing some JSON with JSON.NET

查看:175
本文介绍了一些反序列化JSON与JSON.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是很新的JSON,我需要分析一些API是提供。快速谷歌搜索止跌回升 JSON.NET ,所以我想用它现在这个JSON解析成一个列表对象。 ?首先,是JSON.NET最好的图书馆使用这个

I'm very new to JSON, and I need to parse some that an API is providing. A quick google search turned up JSON.NET, so I'm trying to use it now to parse this JSON into a list object. First of all, is JSON.NET the best library to use for this?

这是我想要做的事:我有一个类名为项目,例如。 JSON的有许多的元素(如果那是他们叫什么),而每个包含3个字段:名为id一个整数,字符串命名的名字,和一个名为CREATIONTIME日期时间。我想一切从JSON 这些项目的元素的解析为项目对象的列表。我已经创建了项目类3场相匹配的JSON。这可怎么使用JSON.NET做什么?

This is what I'm trying to do: I have a class called Item, for example. The json has many "elements" (if that's what they are called), and each contains 3 fields: an integer named id, a string named name, and a datetime named creationTime. I would like to parse all of these Item "elements" from the json into a list of Item objects. I have created 3 fields in the Item class to match the JSON. How can this be done using JSON.NET?

我试过:

List<Item> fav = (List<Item>)new JsonSerializer().Deserialize(new JsonReader((TextReader)new StreamReader(response.GetResponseStream())));



但它似乎并没有工作。我得到一个错误的铸造 - 它只是不能处理成一个列表的外壳,但我甚至不能确定它是否能够在JSON处理成一个项目类(JSON.NET不是很充分证明,但我我要去调试巨资明天)。

but it doesn't seem to work. I get a casting error - it just can't process it into a list enclosure, but I'm not even sure whether it's able to process the JSON into one Item class (JSON.NET is not very well-documented, but I'm going to heavily debug it tomorrow).

您能给我一些示例代码来解释我如何与JSON.NET解析呢?

Can you give me some sample code to explain how I can parse it with JSON.NET?

感谢

更新:对了,忘了提 - 我的项目将被定位。 NET Framework 2.0的,所以我用JSON.NET的旧版本:1.3.1。 ?有没有巨大的优势,可能使项目价值转换为.NET 3.5,而破坏的最低系统要求

UPDATE: By the way, forgot to mention - my project is going to be targeting .NET Framework 2.0, so I'm using the legacy version of JSON.NET: 1.3.1. Are there any HUGE advantages that may make the project worth converting to .NET 3.5, while undermining the minimal system requirements?

更新#2:我决定使用System.Web.Extensions.dll代替JSON.NET的类的JavaScriptSerializer和有关问题发布的此处。谢谢!

UPDATE #2: I have decided to use the JavascriptSerializer class in System.Web.Extensions.dll instead of JSON.NET, and a question about that is posted here. Thanks!

推荐答案

我会建议您升级到.NET Framework 3.5,并使用ASP.Net MVC建立您的JSON服务。参见:

I would recomend that you upgrade to .net framework 3.5 and use ASP.Net MVC to build your json services. See:

http://msmvps.com/blogs/omar/archive/2008/10/03/create- REST的API,使用-ASP净MVC-了 - 讲 - 包括-JSON-和纯xml.aspx

这篇关于一些反序列化JSON与JSON.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆