采用动态解析JSON字符串到字符串在C#JSON.NET [英] Parsing dynamic JSON string into string in C# using JSON.NET

查看:357
本文介绍了采用动态解析JSON字符串到字符串在C#JSON.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C#和JSON我的第一个小项目。有人问我解析一个JSON文件到以下内容:
我试图创建一个Windows形式,其身体将包含一个JSON字符串在以下格式的内容:

This is my first little project on C# and JSON. I'm asked to parse a JSON file into the following: I'm trying to create a windows form whose body will contain the content of a JSON string in the following format:

Name of the object
(Label) name of the attribute of the object - (TextBox) editable value
(Label) name of the attribute of the object - (TextBox) editable value
...

我有大约35属性在每JSON文件和8个目标对象。总共有大约50个不同的属性。我已经寻找JSON - C# - JSON.NET和阅读超过15道题&安培;答案。答案有像我这样的初学者一些有价值的信息,但我不能跟我的情况的答案相关联。这些都是原因:

I have approx 35 attributes per object in the json file and 8 objects. There are around 50 different attributes in total. I have searched for JSON - C# - JSON.NET and read more than 15 questions & answers. Answers had some valuable information for starters like me, but I could not associate the answers with my situation. These are the reasons:


  • 一些答案​​转换的JSON字符串到C#对象。因为我需要的对象的属性的名称,此选项没有打我作为一个解决方案。我不知道如何获取变量名的源代码,并在Windows窗体使用它

  • 一些答案​​转换的JSON字符串<字典> 数据结构。我不完全了解字典及其属性,而是由定义,我认为,词典2值映射到对方。就我而言,3-5个不同的对象可能有相同的属性,因此对于一个属性会有多个值。

  • Some answers converted the json string into C# objects. Since I need the "names" of the attributes of the objects, this option did not strike me as a solution. I have no idea how to get the name of the variable in the source code and use it in the windows form.
  • Some answers converted the json string into <Dictionary> data structure. I'm not fully aware of dictionaries and their properties, but by definition i think, dictionaries map 2 values to each other. In my case, 3-5 different objects may have the same attribute, therefore for one attribute there will be multiple values.

除此之外,我见过一个例子是:

Besides that, an example I've seen was:

< 。code> VAR字典=新的JavaScriptSerializer()反序列化<&字典LT;字符串对象>>(JSON);
VAR POSTALCODE =字典[邮编];

由于我有大约50属性,使用这种方法是不是一种选择对我来说:使用的字段名称一个个就像是在这个例子中完成的(邮编是在引用问题的例子JSON对象的属性之一)

Since I have around 50 attributes, using this approach is not an option for me: Using the field names one by one like it was done in this example ("postalcode" was one of the attributes in the example json object in the referenced question).

第一眼,我想我可能会分析它在我自己的使用字符串操作。但是,我想用美丽的JSON.NET库。我那种坚持在这里,不知道该如何获得JSON对象的属性的名称和Windows表单中使用它和它的价值。我在我的脑海里的一些想法,但不知道如何实现它们。

At the first glance, I thought I might parse it on my own using string manipulation. But I want to use the beautiful JSON.NET library. I'm sort of stuck here, having no idea how to get the name of the attribute of the json object and use it and its value in the windows form. I have some ideas in my mind but have no idea how to implement them.

我可以分析包含8个对象到一个对象数组JSON字符串,包含2D的每个对象字符串数组为他们的属性名和值。那么我就值编辑时字符串转换为浮动。我需要他们为字符串,因为我想在Windows窗体使用它们。这是我心目中的解决方案。我该如何进行呢?

I might parse the json string that contains 8 objects into an object array, each object containing 2D string arrays for their attribute name and value. Then I will convert string into float when editing the value. I need them as strings because I want to use them in the windows form. This is my solution in mind. How can I proceed?

推荐答案

既然你有一个动态的JSON字符串,我建议你解析字符串作为 JObject 。这给了极大的灵活性,只要处理JSON对象树:

Since you have a dynamic json string, I'd recommend parsing the string as a JObject. This gives ultimate flexibility as far as processing the JSON object tree:

var parseTree = JsonConvert.DeserializeObject<JObject>("{ a: 2, b: \"a string\", c: 1.75 }");
foreach (var prop in parseTree.Properties()) {
    Console.WriteLine(prop.Name + ": " + prop.Value.ToObject<object>());
}



另一个 JObject 例子

var parsed = JsonConvert.DeserializeObject<JObject>("{\"name\":{\"a\":2, \"b\":\"a string\", \"c\":3 } }");
foreach (var property in parsed.Properties()) {
    Console.WriteLine(property.Name);
    foreach (var innerProperty in ((JObject)property.Value).Properties()) {
        Console.WriteLine("\t{0}: {1}", innerProperty.Name, innerProperty.Value.ToObject<object>());
    }
}

如果你知道你正在处理一个JSON数组,而是可以解析为 JArray ,然后看看每个 JObject 数组中的:

If you know that you are dealing with a JSON array, you can instead parse as JArray and then look at each JObject in the array:

var properties = JsonConvert.DeserializeObject<JArray>("[{a:1}, {b:2, c:3 }]")
    .SelectMany(o => ((JObject)o).Properties())
    .ToArray();
foreach (var prop in properties) {
    Console.WriteLine(prop.Name + ": " + prop.Value.ToObject<object>());
}

有关更大的灵活性,可以解析为一个 JToken

For even more flexibility, you can parse as a JToken.

这篇关于采用动态解析JSON字符串到字符串在C#JSON.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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