C#获取JSON值 [英] C# Get JSON value

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

问题描述

我正在尝试解析一些JSON,但是我遇到了一个问题.我正在尝试获取商品的名称. JSON: http://steamcommunity.com/id/xejuicy/inventory/json/730/2/

I'm trying to parse some JSON, but I'm running into a problem. I'm trying to get the name of the item. JSON: http://steamcommunity.com/id/xejuicy/inventory/json/730/2/

我尝试的代码:

WebClient wc = new WebClient();
string theItems = wc.DownloadString(string.Format("http://steamcommunity.com/profiles/{0}/inventory/json/730/2/", steamUser.SteamID.ConvertToUInt64()));
dynamic dynObj = JsonConvert.DeserializeObject(theItems);
Console.WriteLine("{0}", dynObj.rgDescriptions,dynObj);
foreach (var name in dynObj)
{
    foreach (var subname in name)
    {
        Console.WriteLine("{0}", subname.name);
    }
}

如果您熟悉CS:GO,我想从蒸汽中获取所有CS:GO物品(甚至是板条箱),我从库存中获得了该游戏的所有物品,并获得了名字.

If you are familiar with CS:GO, i want to get all CS:GO items from steam (even crates), everything from that game I have in inventory and get its name.

错误:JValue不包含名称的定义. 发生的地方:第二个foreach(Console.WriteLine("{0}",subname.name);)

Error: JValue doesn't contain definition for name. Where it occurs: second foreach (Console.WriteLine("{0}", subname.name);)

这不是重复的,因为我成功地反序列化了JSON并且可以取值,但不能获得子值...问题已经回答了.

It's not duplicate because i successfully deserialized JSON and can take value but CAN'T get subvalue... Question already answered though.

推荐答案

dynamic dynObj = JsonConvert.DeserializeObject(theItems);
foreach (var desc in dynObj.rgDescriptions)
{
    Console.WriteLine("{0} => {1}", desc.Name, desc.Value.name);
}

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

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