I am making a program which needs to parse a JSON response. Most of the data in the response is useless but I need to do a few things with it.
- Be able to retrieve the search for the
id
from thergInventory
section, then get the correspondingclassid
- Get the
market_name
for the object with thisclassid
JSON Response (snippet, it continues on with that format):
{
"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
},
},
"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":"The Bank Collection",
"color":"9da1a9",
"app_data":{
"def_index":"65535",
"is_itemset_name":1
}
},
],
"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"
},
]
}
}
}
Full JSON response: http://steamcommunity.com/id/Mambocsgoshack/inventory/json/730/2/
I believe because the identifiers keep changing for each item in the inventory (i.e. 469449975_0
to 619638799_0
) I would have to deserialize this to a dictionary.
Here is my code thus far:
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);
Dictionary<string, Item> result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Item>>(response);
return new CSGOInventory(result);
}
public Item[] Items { get; set; }
public bool IsPrivate { get; private set; }
public bool IsGood { get; private set; }
protected CSGOInventory(Dictionary<string, Item> apiInventory)
{
for (int i = 0; i < apiInventory.Count; i++)
{
Items[i] = apiInventory.Values.ElementAt(i);
}
}
/*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 string AppId = "730";
public string ContextId = "2";
[JsonProperty("instanceid")]
public string instanceid { get; set; }
[JsonProperty("market_name")]
public string market_name { get; set; }
[JsonProperty("def_index")]
public string def_index { get; set; }
}
protected class InventoryResult
{
public Item[] items { get; set; }
}
protected class InventoryResponse
{
public InventoryResult result;
}
}
}
I believe I am adding the dictionary items to the Items array completely wrong but cannot figure out the correct solution.
However, the error currently is:
Error converting value True to type 'SteamTrade.CSGOInventory+Item , Path 'success', line 1, position 15.
I sort of understand what this means but do not know how to work around it. I didn't think I had to define every property that the JSON returns within my object, but I could well be wrong. Either way, since the format of the JSON changes from the rgInventory
to rgDescriptions
sections changes I do not know how to address this. Could anyone explain how to do this?
UPDATE:
My method to retrieve instanceid from market_name is as follows:
public string getInstanceIdFromMarketName(string name)
{
var classIdToId = inventory.rgInventory.ToLookup(pair => pair.Value.classid, pair => pair.Key);
var marketNameToId = inventory.rgDescriptions
.SelectMany(pair => classIdToId[pair.Value.classid].Select(id => new KeyValuePair<string, string>(pair.Value.market_name, id)))
.ToLookup(pair => pair.Key, pair => pair.Value);
if (marketNameToId[name].First() != null)
{
string idForMarket = marketNameToId[name].FirstOrDefault();
return idForMarket;
}
else
{
return null;
}
}
This returns an error saying there are no items in the sequence.