如何在C#中使用Json.net解析未知的Json文件

我可以使用获取动态 Json 对象

dynamic obj = JsonConvert.DeserializeObject(json);

看起来是一个嵌套的对象结构

我需要绘制 json 文件中的每个变量的图表,但 json 文件结构经常发生变化

有没有办法使用嵌套的 foreach() 语句来解析这个结构?如果没有,我可以通过像字典一样的字符串访问每个元素来解析它吗?

例如:

if(obj["Item1"]["Parameter3"]["Value2"]` != NULL)
   int number = obj["Item1"]["Parameter3"]["Value2"]`

谢谢,


杨__羊羊
浏览 149回答 2
2回答

一只甜甜圈

是的,有一个用于动态查询的 API。代码看起来像这样:JObject rss = JObject.Parse(json); var postTitles =    from p in rss["channel"]["item"]    select (string)p["title"];

POPMUISE

终于搞清楚这个api了一些 JToken 条目具有值列表,其他条目具有名称和值。在解析之前,您必须先对哪个是哪个进行排序。这将创建一个包含 Json 文件中每个条目的字典&nbsp; &nbsp; void SomeFunction()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Dictionary<string, decimal> json_data = new Dictionary<string, decimal>();&nbsp; &nbsp; &nbsp; &nbsp; dynamic json_obj = JsonConvert.DeserializeObject(json);&nbsp; &nbsp; &nbsp; &nbsp; Linearize(ref json_data, json_obj);&nbsp; &nbsp; }&nbsp; &nbsp; void Linearize(ref Dictionary<string, decimal> input_dict, JToken json_data, string key = "")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int i;&nbsp; &nbsp; &nbsp; &nbsp; if (json_data != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (json_data.HasValues)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (dynamic entry in json_data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add a Name field if it exists&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type typeOfDynamic = entry.GetType();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeOfDynamic.GetProperties().Where(p => p.Name.Equals("Name")).Any())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key += entry.Name + ".";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If JToken is an Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (((JToken)entry).HasValues)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Linearize(ref input_dict, entry, key + "[" + i++ + "]" + ".");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If JToken is a data type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (entry.Type == JTokenType.String || entry.Type == JTokenType.Float || entry.Type == JTokenType.Integer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decimal output;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (decimal.TryParse(entry.ToString(), out output))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input_dict.Add(key + "[" + i++ + "]", output);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP