C# 如何通过 JObject 进行枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10543512/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:15:00  来源:igfitidea点击:

How do I enumerate through a JObject?

c#jsonjson.net

提问by Geesu

I'm trying to determine how to access the data that is in my JObject and I can't for the life of me determine how to use it.

我正在尝试确定如何访问 JObject 中的数据,但我终生无法确定如何使用它。

JObject Object = (JObject)Response.Data["my_key"];

I can print it to the console doing Console.WriteLine(Object) and I see the data, it looks like:

我可以将它打印到控制台执行 Console.WriteLine(Object) 并且我看到数据,它看起来像:

{
 "my_data" : "more of my string data"
...
}

But I have NO idea how to just iterate/enumerate through it, anyone have any ideas? I'm at such a loss right now.

但我不知道如何迭代/枚举它,有人有什么想法吗?我现在很失落。

采纳答案by svick

If you look at the documentation for JObject, you will see that it implements IEnumerable<KeyValuePair<string, JToken>>. So, you can iterate over it simply using a foreach:

如果您查看的文档JObject,您将看到它实现了IEnumerable<KeyValuePair<string, JToken>>. 因此,您可以简单地使用 a 对其进行迭代foreach

foreach (var x in obj)
{
    string name = x.Key;
    JToken value = x.Value;
    …
}

回答by jaxxbo

The answer did not work for me. I dont know how it got so many votes. Though it helped in pointing me in a direction.

答案对我不起作用。我不知道它是怎么得到这么多票的。虽然它帮助我指明了一个方向。

This is the answer that worked for me:

这是对我有用的答案:

foreach (var x in jobj)
{
    var key = ((JProperty) (x)).Name;
    var jvalue = ((JProperty)(x)).Value ;
}

回答by Daniel

JObjectscan be enumerated via JPropertyobjects by casting it to a JToken:

JObjects可以通过JProperty对象通过将其转换为JToken来枚举:

foreach (JProperty x in (JToken)obj) { // if 'obj' is a JObject
    string name = x.Name;
    JToken value = x.Value;
}

If you have a nested JObject inside of another JObject, you don't need to cast because the accessor will return a JToken:

如果在另一个 JObject 中有嵌套的 JObject,则不需要强制转换,因为访问器将返回 JToken:

foreach (JProperty x in obj["otherObject"]) { // Where 'obj' and 'obj["otherObject"]' are both JObjects
    string name = x.Name;
    JToken value = x.Value;
}

回答by dani herrera

For people like me, linq addicts, and based on svick's answer, here a linq approach:

对于像我这样的linq 瘾君子,根据 svick 的回答,这里有一个 linq 方法:

using System.Linq;
//...
//make it linq iterable. 
var obj_linq = Response.Cast<KeyValuePair<string, JToken>>();

Now you can make linq expressions like:

现在您可以制作 linq 表达式,例如:

JToken x = obj_linq
          .Where( d => d.Key == "my_key")
          .Select(v => v)
          .FirstOrDefault()
          .Value;
string y = ((JValue)x).Value;

Or just:

要不就:

var y = obj_linq
       .Where(d => d.Key == "my_key")
       .Select(v => ((JValue)v.Value).Value)
       .FirstOrDefault();

Or this one to iterate over all data:

或者这个迭代所有数据:

obj_linq.ToList().ForEach( x => { do stuff } );