简体   繁体   English

您如何解析没有定义结构的JSON.NET文件?

[英]How do you parse a JSON.NET file without a defined structure?

I'm using an API from OpenCalais that gives me a json string that contains a bunch of JSON objects. 我正在使用来自OpenCalais的API,该API给了我一个包含一堆JSON对象的json字符串。 The problem is that depending on what I pass into the API the structure of the JSON string changes, which means that I can't cast the deseralization against a custom class I could make. 问题是,取决于我传递给API的内容,JSON字符串的结构会发生变化,这意味着我无法将反序列化转换为我可以创建的自定义类。 Another problem is that the output isn't in key value form, but rather a complicated object structure. 另一个问题是输出不是键值形式,而是复杂的对象结构。 Here is an example of the output- 这是输出示例

{"doc":
{"info":
{"allowDistribution":"true",
"allowSearch":"true",
"calaisRequestID":"c1cdd79a-ed89-8431-138c-50e8a37100f9",
"externalID":"17cabs901",
"id":"http://id.opencalais.com/0RCcU306*HTR05*7HlUb5A",
"docId":"http://d.opencalais.com/dochash-1/6188237f-a2a5-3263-95b7-ea894ba98298",
"document":"Bill Gates worked at Microsoft from 2008 to 2011. He is also married to Melinda Gates.",
"docTitle":"",
"docDate":"2012-07-26 15:51:00.885",
"externalMetadata":"",
"submitter":"ABC"}

So you can see there are multiples levels of objects here and there is no key value structure. 因此,您可以看到这里有多个级别的对象,并且没有键值结构。 What I need from this code is essentially all of the name/value info in a dictionary. 我需要的这段代码本质上是字典中的所有名称/值信息。 Then I can sort through based on the keys to the relevant info. 然后,我可以根据相关信息的键进行排序。 It would also be cool if I could somehow also grab the parent of the object value. 如果我能以某种方式也抓住对象值的父对象,那也很酷。 So for example the output I want is key: allowSearch, value: true, parent: info. 因此,例如,我想要的输出是键:allowSearch,值:true,父级:info。 I have been trying to mess around with different method of deserialization, dynamic objects, and just plain parsing using functions like .Ancestor and I haven't found anything useful. 我一直在尝试使用反序列化,动态对象的不同方法,并使用.Ancestor之类的函数进行简单解析,但我没有发现任何有用的方法。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Have you looked at JSON.NET ? 您是否看过JSON.NET It can dynamically parse JSON which you can then iterate through: 它可以动态解析JSON,然后您可以通过以下方式进行迭代:

using Newtonsoft.Json.Linq;

JObject rootObject = JObject.Parse(jsonString);

You can iterate through the children, or search for other nodes like this: 您可以遍历子节点,或搜索其他节点,如下所示:

JToken info = rootObject.SelectToken("info");

Json.NET support LINQ to JSON under the Newtonsoft.Json.Linq namespace. Json.NET在Newtonsoft.Json.Linq命名空间下支持LINQ to JSON

The example to use JObject 使用JObject 的示例

JObject 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<string> allDrives = o["Drives"].Select(t => (string)t).ToList();
// DVD read/writer
// 500 gigabyte hard drive

You can use DeserializeObject which essentially returns a Dictionary<string, object> . 您可以使用DeserializeObject ,它实质上返回Dictionary<string, object>

For example: 例如:

JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> myGraph = (Dictionary<string, object>)serializer.DeserializeObject(myJson);

To read stuff from the myGraph dictionary, you will have to cast each step of the way. 要从myGraph词典中读取内容,您将必须myGraph转换每一步。

For example: 例如:

Dictionary<string, object> doc = (Dictionary<string, object>)myGraph["doc"];
Dictionary<string, object> info = (Dictionary<string, object>)doc["info"];
string externalID = (string)info["externalID"];

I'm sure you could genericize it a bit, but I think that should work with the json you posted. 我确定您可以对其进行泛化,但是我认为这应该与您发布的json一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM