我怎样才能浏览在C#中的任何JSON的树? [英] How can I navigate any JSON tree in c#?

查看:199
本文介绍了我怎样才能浏览在C#中的任何JSON的树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要导航的JSON结构,我会用浏览一个XML 的XmlDocument

I need to navigate a Json structure as I would navigate an XML using XmlDocument.

结构是不知道的,我需要遍历节点来分析一些数据。

The structure is not known, and I need to iterate over the nodes to parse some data.

这可能吗?
我知道我可以使用 JavaScriptSerializer 反序列化到一个已知类型的,但是这不是我可以接受任何有效的JSON的情况。

Is this possible?
I know I can use JavaScriptSerializer to deserialize it into a known type, but this is not the case as I can receive any valid json.

<打击>我使用.NET 3.5(SP1),并不能在此刻升级到4.0。
我升级到.NET 4.0使用动态类型(这是由code迷死)

I'm using .NET 3.5 (SP1) and cannot upgrade to 4.0 at the moment.
I upgraded to .NET 4.0 to use dynamic types (which is awesomeness made code)

推荐答案

阅读这篇文章:

  • <一个href="http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx" rel="nofollow">http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx

这说明你解析JSON到具有字典里面动态对象的一种方式。

It explains you a way of parsing JSON to a dynamic object which has a dictionary inside.

所以,迭代字典将是很好的LINQ,不会呢?

So, iterating a dictionary would be nice with LINQ, wouldn't be?

- 或者,如果你在.NET 3.5 ... ---;)

--- OR IF YOU'RE IN .NET 3.5... --- ;)

你为什么不实现象ToDictionary扩展方法?

Why don't you implement an extension method like "ToDictionary"?

您可以接收JSON文本,后来与一个普通的前pression和拆分的属性和值到字典解析,完成这一切与建议的扩展方法。

You can receive JSON text, later parse with a regular expression and split properties and values into a dictionary, everything done with suggested extension method.

它如何工作的一个样本:

A sample of how it would work that:

IDictionary<string, object> deserializedJson = jsonText.ToDictionary();

适合您的需求?

Fits your needs?

---甚至还没有完全再试一次(现在你更多的选择)! ---

--- EVEN YET ANOTHER TRY (now you've more options)! ---

检查这个开源项目在codePLEX:

Check this open source project on CodePlex:

它LINQ到JSON,所以你可以读写JSON。

It has LINQ-to-JSON so you can read and write JSON.

这篇关于我怎样才能浏览在C#中的任何JSON的树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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