[英]How would I parse this JSON response
我需要解析鏈接在這篇文章底部的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/ 個人資料 /“ + steamId +” / inventory / json / 730/2 / http://steamcommunity.com/ id / Mambocsgoshack / inventory / json / 730/2 /
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.