如何反序列化使用未指定和可变属性名称的JSON对象 [英] How can I deserialize a JSON object that uses unspecified and variable property names
本文介绍了如何反序列化使用未指定和可变属性名称的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似于以下内容的JSON响应(我无法控制):
I have a JSON response (which I have no control over) similar to this:
{"response":{
"a" : "value of a",
"b" : "value of b",
"c" : "value of c",
...
}}
位置:
- "a","b","c"是预先未知的名称.
- 物品的数量可以变化.
我最后需要的是所有值的字符串数组.保留名称是一种奖励(词典?),但我需要按它们出现的顺序浏览值.
All I need at the end is an array of strings for all the values. Keeping the names is a bonus (Dictionary?) but I need to browse values by the order in which they appear.
如何使用JSON.NET实现此目标?
How would you achieve this using JSON.NET?
推荐答案
您可以使用Newtonsoft.Json.Linq
命名空间中的JObject
类将对象反序列化为类似DOM的结构:
You can use the JObject
class from the Newtonsoft.Json.Linq
namespace to deserialize the object into a DOM-like structure:
public class StackOverflow_10608188
{
public static void Test()
{
string json = @"{""response"":{
""a"" : ""value of a"",
""b"" : ""value of b"",
""c"" : ""value of c""
}}";
JObject jo = JObject.Parse(json);
foreach (JProperty property in jo["response"].Children())
{
Console.WriteLine(property.Value);
}
}
}
这篇关于如何反序列化使用未指定和可变属性名称的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文