使用 System.Json 遍历 JSON [英] Iterating through JSON using System.Json

查看:25
本文介绍了使用 System.Json 遍历 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索 .NET 4.5 System.Json 库的功能,但没有太多文档,而且由于流行的 JSON.NET 库,搜索起来非常棘手.

我基本上想知道,例如,我将如何遍历一些 JSON:

{ "People": { "Simon" : { Age: 25 }, "Steve" : { Age: 15 } } }

我有一个字符串形式的 JSON,我想遍历并显示每个人的年龄.

所以首先我会这样做:

var jsonObject = JsonObject.Parse(myString);

但是接下来我不知道该怎么做.我很惊讶 parse 方法返回的是 JsonValue 而不是 JsonObject.

我真正想做的是:

foreach(jsonObject.Children 中的 var child){if (child.name == "人"){//另一个 foreach 循环遍历人//获取他们的姓名和年龄,例如.person.Name 和 person.Children.Age(LINQ 这个或什么)}}

有什么想法吗?

解决方案

由于接受的答案对我没有多大帮助,因为它的典型用途之一是使用这个库更好!"答案我想通了.

是的,

JsonObject.Parse(myJsonString);

返回一个 JsonValue 对象,它是 System.Json 中所有 Json* 类的基类.当您调用 JsonObject.Parse(..) 时,JsonValue.Parse() 因为继承而真正被调用.

要遍历您可以使用的 JsonObject:

var jsonObject = JsonValue.parse(jsonString);foreach (KeyValuePair jsonObject 中的值){Console.WriteLine("key:" + value.Key);Console.WriteLine("value:" + value.Value);}

如果它是一个 JsonArray,我没有尝试过这是否也有效,但也许如果它是一个 JsonArray 那么你可能想用经典的 i 来做它:

for (var i = 0; i 

如果你不知道它是一个数组还是一个对象,那么你可以在此之前检查

if (jsonObject.GetType() == typeof JsonObject){//像对象一样迭代}else if (jsonObject.GetType() == typeof JsonArray){//像数组一样迭代}

I'm exploring the capabilities of .NET 4.5 System.Json library but there isn't much documentation, and it's quite tricky to search for due to the popular JSON.NET library.

I am wondering basically, how I'd loop through some JSON for example:

{ "People": { "Simon" : { Age: 25 }, "Steve" : { Age: 15 } } }

I have my JSON in a string, and I want to iterate through and display the ages of everyone.

So first I'd do:

var jsonObject = JsonObject.Parse(myString);

but then I'm at a loss of what to do next. I'm surprised that the parse method returns a JsonValue not a JsonObject.

What I want to do really is:

foreach (var child in jsonObject.Children)
{
  if (child.name == "People")
{
 // another foreach to loop over the people
 // get their name and age, eg. person.Name and person.Children.Age (LINQ this or something)

}

}

any ideas?

解决方案

Since the accepted answer does not helped me much because its one of the typcial useles "use this lib its better!" answers i figured that out.

Yes,

JsonObject.Parse(myJsonString);

Returns a JsonValue object that is the base class of all the Json* classes from System.Json. When you call JsonObject.Parse(..) then JsonValue.Parse() is really being called because of the inheritance.

to iterate over a JsonObject you can use:

var jsonObject = JsonValue.parse(jsonString);

foreach (KeyValuePair<string, JsonValue> value in jsonObject)
{
    Console.WriteLine("key:" + value.Key);
    Console.WriteLine("value:" + value.Value);
}

I dont tried out if this also works if its an JsonArray but maybe if its an JsonArray then you might want to do it with a classic for i:

for (var i = 0; i < jsonObject.Count; i++)
{
    Console.WriteLine("value:" + jsonObject[i]);
}

If you dont know if its an array or an object than you might check that before

if (jsonObject.GetType() == typeof JsonObject){
    //Iterate like an object 
}else if (jsonObject.GetType() == typeof JsonArray){
    //iterate like an array
}

这篇关于使用 System.Json 遍历 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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