C# Get JSON value [duplicate]

隐身守侯 提交于 2019-12-12 06:03:55

问题


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/

Code I tried:

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);
    }
}

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.

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

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


回答1:


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


来源:https://stackoverflow.com/questions/32466119/c-sharp-get-json-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!