C# 在 JSON.Net 4.0 中使用 JObject 和 JProperty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10542810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using JObject and JProperty with JSON.Net 4.0
提问by Greg
I'm trying to deserialize JSON in this format:
我正在尝试以这种格式反序列化 JSON:
{
"data": [
{
"installed": 1,
"user_likes": 1,
"user_education_history": 1,
"friends_education_history": 1,
"bookmarked": 1
}
]
}
to a simple string array like this:
到一个像这样的简单字符串数组:
{
"installed",
"user_likes",
"user_education_history",
"friends_education_history",
"bookmarked"
}
using JSON.NET 4.0
使用 JSON.NET 4.0
I've gotten it to work using the `CustomCreationConverter'
我已经使用“CustomCreationConverter”让它工作了
public class ListConverter : CustomCreationConverter<List<string>>
{
public override List<string> Create(Type objectType)
{
return new List<string>();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var lst = new List<string>();
//don't care about the inital 'data' element
reader.Read();
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName)
{
lst.Add(reader.Value.ToString());
}
}
return lst;
}
}
but this really seems like overkill, especially if I want to create one for many different json responses.
但这真的看起来有点矫枉过正,特别是如果我想为许多不同的 json 响应创建一个。
I've tried using JObjectbut it doesn't seem like I'm doing it right:
我试过使用,JObject但似乎我做得不对:
List<string> lst = new List<string>();
JObject j = JObject.Parse(json_string);
foreach (JProperty p in j.SelectToken("data").Children().Children())
{
lst.Add(p.Name);
}
Is there a better way to do this?
有一个更好的方法吗?
采纳答案by carlosfigueira
There are many ways you can do that, and what you have is fine. A few other alternatives are shown below:
有很多方法可以做到这一点,你所拥有的一切都很好。下面显示了其他一些替代方案:
- Get the first element of the array, instead of all the children
Use
SelectTokento go to the first array element with a single callstring json = @"{ ""data"": [ { ""installed"": 1, ""user_likes"": 1, ""user_education_history"": 1, ""friends_education_history"": 1, ""bookmarked"": 1 } ] }"; JObject j = JObject.Parse(json); // Directly traversing the graph var lst = j["data"][0].Select(jp => ((JProperty)jp).Name).ToList(); Console.WriteLine(string.Join("--", lst)); // Using SelectToken lst = j.SelectToken("data[0]").Children<JProperty>().Select(p => p.Name).ToList(); Console.WriteLine(string.Join("--", lst));
- 获取数组的第一个元素,而不是所有子元素
用于
SelectToken通过一次调用转到第一个数组元素string json = @"{ ""data"": [ { ""installed"": 1, ""user_likes"": 1, ""user_education_history"": 1, ""friends_education_history"": 1, ""bookmarked"": 1 } ] }"; JObject j = JObject.Parse(json); // Directly traversing the graph var lst = j["data"][0].Select(jp => ((JProperty)jp).Name).ToList(); Console.WriteLine(string.Join("--", lst)); // Using SelectToken lst = j.SelectToken("data[0]").Children<JProperty>().Select(p => p.Name).ToList(); Console.WriteLine(string.Join("--", lst));

