JSON在.NET C#/ Csharp的 - 获取值出对象 [英] JSON in .NET C#/Csharp - Getting values out of the object

查看:540
本文介绍了JSON在.NET C#/ Csharp的 - 获取值出对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要分析一些JSON数据。我使用詹姆斯·牛顿 - 国王JSON.NET库

I want to parse some JSON data. I'm using James Newton-King's JSON.NET library.

我解析JSON字符串转换为JObject。这里是JSON我解析:

I parse the JSON string into a JObject. Here is the JSON I'm parsing:

"root": [
{
  "date": 1325400000000,
  "id": 12313131,
  "loc": "en_us",
  "name": "New York, NY",
  "products": [
    {
      "@type": "asdf",
      "city": "New York - Penn Station, NY (NYP)",
      "code": "USA",
    }
  ],
  "summary": {
    "alert": [],
    "end": 1325577000000,
    "start": 1325400000000
  }
}
]
}

正如你可以看到它的pretty的复杂。 根是必要的,因为otherwhise数据不能被解析成JObject实例。

As you can see it's pretty complex. The "root" was necessary because otherwhise the data could not be parsed into a JObject instance.

JObject o = JObject.Parse(jsonString);

JSON数据是相当大的。窗口里有多个项目进行不同的ID。我想找到一个specifid ID的项目。

The JSON data is quite large. There are multiple items in it with different IDs. I want to find an item with a specifid ID.

现在的问题是,当我尝试的foreach通过数据它只有一个元素。

The problem is, when I try to foreach through the data it has only one element.

KEY: root
VALUE: the other stuff.

那么,如何才能获得通过里面有什么其他的东西和周期?

So how do I get to the other stuff and cycle through what's inside?

推荐答案

没关系..

我只是解决了它。

我删除了尾随[和结束。

I removed the trailing [ and the end ].

所以,现在是一个有效的JSON对象和关键值的foreach是工作就像一个魅力。

So it is now a Valid Json object and Key Value foreach is working like a charm..

            foreach (KeyValuePair<String, JToken> d in o)
            {
                Console.WriteLine(String.Format("Key: {0}; Value: {1}", d.Key, d.Value));
            }

万岁!

原来这只能解决部分问题。因为现在的人则不仅格式化第一段是。其他人莫名其妙地消失了...:妈s这个...

Turned out this is only a partial solution. Because now the others are not formatted only the first segment is. The others somehow disappear... :S Damn this...

甚至更好的解决办法...我是个白痴......

Even better solution... I was a complete idiot...

让一切到位,只使用JArray JA = JArray.Parse(stringOfJson);

Leave everything in place and simply use JArray ja = JArray.Parse(stringOfJson);

这会给你一个数组满了全线贯通......真棒自由周期的数据。 :)

This will give you an array full with all the data free to cycle through... Awesome. :)

这篇关于JSON在.NET C#/ Csharp的 - 获取值出对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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