我有大约7000行要解析的JSON数据.可以看到其中一部分的示例.我所做的是使用WebRequest和StreamReader将所有数据放入字符串中. (奇怪的是,它将所有数据放入一条很长的行中).但是现在我想解析这个,我不确定如何.谁能解释如何使用Deserialize?我之前已经用Java解析过JSON数据,但是用C#却遇到了麻烦,特别是因为我无法找到带有清晰示例的文档.任何帮助将不胜感激.
I have about 7000 lines of JSON data that I want to parse. An example of just part of it can be seen here. What I did was use WebRequest and StreamReader to put all the data into a string. (Oddly, it puts all of the data into one VERY long line). But now I want to parse this and I am not sure how. Can anyone explain how to use Deserialize? I have parsed JSON data with Java before but I am having trouble doing so with C# especially with my inability to find documentation with clear examples. Any help will be greatly appreciated.
推荐答案如果有,请尝试 JSON.Net 没看到这对您有帮助.
Try JSON.Net, if you have not seen this it should help you.
Json.NET库可以与 .NET中JSON格式的数据很简单. 主要功能包括灵活的JSON 序列化器,用于快速转换 .NET类转换为JSON并再次返回, 和LINQ to JSON进行读取和 编写JSON.
Json.NET library makes working with JSON formatted data in .NET simple. Key features include a flexible JSON serializer to for quickly converting .NET classes to JSON and back again, and LINQ to JSON for reading and writing JSON.
反序列化已在此处进行了讨论.
Deserialization discussed here.
最快的转换方法 JSON文本和.NET对象之间是 使用JsonSerializer.这 JsonSerializer转换.NET对象 转换成它们的JSON等价物并返回 再次.
The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again.
反序列化的基本代码结构如下-Target仍需要填写,以捕获具有适当类型的其余已解析数据项.提到的json.txt文件包含来自上面URL的数据.
The basic code structure for deserialization is below - Target still needs to be filled out to capture the rest of the parsed data items with the appropriate type. The file mentioned json.txt contains your data from the URL above.
using System; using System.IO; using Newtonsoft.Json; public class NameAndId { public string name; public int id; } public class Data { public NameAndId[] data; } public class Target { public string id; public NameAndId from; public Data likes; } public class Program { static void Main(string[] args) { string json = File.ReadAllText(@"c:\temp\json.txt"); Target newTarget = JsonConvert.DeserializeObject<Target>(json); } }这是JSON流的第一部分供参考:
Here is the first part of the JSON stream for reference:
{ "id": "367501354973", "from": { "name": "Bret Taylor", "id": "220439" }, "message": "Pigs run from our house in fear. Tonight, I am wrapping the pork tenderloin in bacon and putting pancetta in the corn.", "updated_time": "2010-03-06T02:57:48+0000", "likes": { "data": [ { "id": "29906278", "name": "Ross Miller" }, { "id": "732777462", "name": "Surjit Padham" },更多推荐
使用C#解析JSON数据
发布评论