我将如何解析此 JSON 响应



我需要解析本文底部链接的JSON响应。我已经尝试了几种非常标准的方法来解析此 JSON,但我相信它不是合法的 JSON。有没有人有任何解决方案可以让我解析这个 JSON?

答复问题:http://steamcommunity.com/id/Mambocsgoshack/inventory/json/730/2/

响应

片段,如果您想查看实际响应,只需单击链接即可。有人告诉我,有些人做不到。

{"success":true,"rgInventory":{"1482735510":{"id":"1482735510","classid":"469449975","instanceid":"0","amount":"1","pos":1},"1468698711":{"id":"1468698711","classid":"619638799","instanceid":"0","amount":"1","pos":2},"1468698710":{"id":"1468698710","classid":"666312349","instanceid":"0","amount":"1","pos":3},"1468698709":{"id":"1468698709","classid":"666310610","instanceid":"0","amount":"1","pos":4},"1468698708":{"id":"1468698708","classid":"614052764","instanceid":"0","amount":"1","pos":5},"1462270322":{"id":"1462270322","classid":"520025252","instanceid":"0","amount":"1","pos":6},"1459818809":{"id":"1459818809","classid":"638240019","instanceid":"0","amount":"1","pos":7},"1450750270":{"id":"1450750270","classid":"621559757","instanceid":"188530139","amount":"1","pos":8},"1391297747":{"id":"1391297747","classid":"384801319","instanceid":"0","amount":"1","pos":9},"1370560151":{"id":"1370560151","classid":"469445126","instanceid":"0","amount":"1","pos":10},"1314553100":{"id":"1314553100","classid":"638240019","instanceid":"0","amount":"1","pos":11},"1305163655":{"id":"1305163655","classid":"469431148","instanceid":"0","amount":"1","pos":12},"1304896559":{"id":"1304896559","classid":"310778121","instanceid":"0","amount":"1","pos":13},"1304062628":{"id":"1304062628","classid":"638240019","instanceid":"0","amount":"1","pos":14},"1214784536":{"id":"1214784536","classid":"575589550","instanceid":"519977179","amount":"1","pos":15},"1201208194":{"id":"1201208194","classid":"360467265","instanceid":"188530670","amount":"1","pos":16},"1189828757":{"id":"1189828757","classid":"519982340","instanceid":"519977179","amount":"1","pos":17},"1103736871":{"id":"1103736871","classid":"638237282","instanceid":"565664004","amount":"1","pos":18},"1103736870":{"id":"1103736870","classid":"638237283","instanceid":"565664004","amount":"1","pos":19},"1103736869":{"id":"1103736869","classid":"742266438","instanceid":"674552754","amount":"1","pos":20},"957595359":{"id":"957595359","classid":"527649910","instanceid":"188530170","amount":"1","pos":21},"814442137":{"id":"814442137","classid":"527678066","instanceid":"188530382","amount":"1","pos":22},"623936007":{"id":"623936007","classid":"469452066","instanceid":"0","amount":"1","pos":23},"616381102":{"id":"616381102","classid":"519985137","instanceid":"519977179","amount":"1","pos":24},"612997861":{"id":"612997861","classid":"616333150","instanceid":"571452803","amount":"1","pos":25},"603041123":{"id":"603041123","classid":"519980785","instanceid":"519977179","amount":"1","pos":26}},"rgCurrency":[],"rgDescriptions":{"469449975_0":{"appid":"730","classid":"469449975","instanceid":"0","icon_url":"fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5oM7bgZghmfzvDE61HY-Yy_QbpNis77893GtbmoLpffljq4tCXNLN9ZY0fSZPVCaWPZQ_5v0tshKIJK5KBqSjs2i73ejBdAx_EB8I","icon_url_large":"fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5oM7bgZghmfzvDE61HY-Yy_QbpNis77893a9u35bwDZ13vs9PPNOQpZoodGMOBD6PVMFr4uRgxg6dZepXdpCm72SrhM2wJXBD1ujVT-Ntzxu8","icon_drag_url":"","name":"SG 553 | Army Sheen","market_hash_name":"SG 553 | Army Sheen (Factory New)","market_name":"SG 553 | Army Sheen (Factory New)","name_color":"D2D2D2","background_color":"","type":"Consumer Grade Rifle","tradable":1,"marketable":1,"commodity":0,"descriptions":[{"type":"html","value":"Exterior: Factory New"},{"type":"html","value":" "},{"type":"html","value":"The terrorist-exclusive SG553 is a premium scoped alternative to the AK47 for effective long-range engagement. It has been covered in a metallic foil stamped with a camouflage pattern."},{"type":"html","value":" "},{"type":"html","value":"The Bank Collection","color":"9da1a9","app_data":{"def_index":"65535","is_itemset_name":1}},{"type":"html","value":" "}],"actions":[{"name":"Inspect in Game...","link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D2486209296654018845"}],"market_actions":[{"name":"Inspect in Game...","link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D2486209296654018845"}],"tags":[{"internal_name":"CSGO_Type_Rifle","name":"Rifle","category":"Type","category_name":"Type"},{"internal_name":"weapon_sg556","name":"SG 553","category":"Weapon","category_name":"Weapon"},{"internal_name":"set_bank","name":"The Bank Collection","category":"ItemSet","category_name":"Collection"},{"internal_name":"normal","name":"Normal","category":"Quality","category_name":"Category"},{"internal_name":"Rarity_Common_Weapon","name":"Consumer Grade","category":"Rarity","color":"b0c3d9","category_name":"Quality"},{"internal_name":"WearCategory0","name":"Factory New","category":"Exterior","category_name":"Exterior"}]},"619638799_0":{"appid":"730","classid

好的,所以我尝试过的基础知识:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SteamKit2;
using System.Net;
namespace SteamTrade
{
    public class CSGOInventory
    {
        public static CSGOInventory FetchInventory(string steamId)
        {
            WebClient client = new WebClient();
            var url = "http://steamcommunity.com/profiles/" + steamId + "/inventory/json/730/2/";
            string response =  client.DownloadString(url);
            InventoryResponse result = Newtonsoft.Json.JsonConvert.DeserializeObject<InventoryResponse>(response);
            return new CSGOInventory(result.result);
        }
        public Item[] Items { get; set; }
        protected CSGOInventory(InventoryResult apiInventory)
        {
            Items = apiInventory.items;
        }
        public Item GetItem(int id)
        {
            return (Items == null ? null : Items.FirstOrDefault(item => item.instanceid == id));
        }
        public List<Item> GetItemsByDefindex(int defindex)
        {
            return Items.Where(item => item.def_index == defindex).ToList();
        }
        public class Item
        {
            public int AppId = 730;
            public long ContextId = 2;
            [JsonProperty("instanceid")]
            public int instanceid { get; set; }
            [JsonProperty("market_name")]
            public string market_name { get; set; }
            [JsonProperty("def_index")]
            public int def_index { get; set; }
        }
        protected class InventoryResult
        {
            public Item[] items { get; set; }
        }
        protected class InventoryResponse
        {
            public InventoryResult result;
        }
    }
}

我得到的错误:

Object reference not set to an instance of an object.

我收到这一行的错误:

protected CSGOInventory(InventoryResult apiInventory)
    {
        Items = apiInventory.items;
    }

我认为我需要将其序列化为字典,但我不确定如何在我的代码中实现它。在这种情况下,任何人都可以建议合适的实施吗?

我只是将该网站的 json 保存到一个文件中,将其作为资源添加到控制台应用程序中,以下代码工作正常

static void Main(string[] args)
{
    string json = Properties.Resources.json;
    var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
}

您要么保存/检索数据不正确,要么使用不合标准的 JSON 库。 尝试NewtonSoft JSON

查看了更新问题中的代码后,我猜数据不正确,因为您使用的是不同的 URL。

http://steamcommunity.com/配置文件/" + 蒸汽 ID + "/库存/json/730/2/http://steamcommunity.com/id/Mambocsgoshack/inventory/json/730/2/

相关内容

  • 没有找到相关文章

最新更新