所以我一直在寻找网络,阅读无数的线程,并且还没有找到我这个小问题的答案。
所以我有一个大的json文件,我需要反序列化为一个对象列表。
目的是使用户可以从列表中搜索项目名称,然后使用该项目的id#搜索api的另一部分,该部分简单地返回该单个项目的信息。 我有其他部分都想通了并且正在工作,但这部分我似乎无法做对。
这是json的一个例子:
{"2": {"name": "Cannonball", "buy_average": 173, "sell_average": 174, "overall_average": 174, "id": 2, "members": true, "sp": 5}, "6": {"name": "Cannon base", "buy_average": 173398, "sell_average": 177731, "overall_average": 186396, "id": 6, "members": true, "sp": 187500}, "12289": {"name": "Mithril platelegs (t)", "buy_average": 2473, "sell_average": 2473, "overall_average": 0, "id": 12289, "members": false, "sp": 2600}, "8": {"name": "Cannon stand", "buy_average": 184003, "sell_average": 184471, "overall_average": 185564, "id": 8, "members": true, "sp": 187500}, "10": {"name": "Cannon barrels", "buy_average": 183594, "sell_average": 185125, "overall_average": 188186, "id": 10, "members": true, "sp": 187500}, "12": {"name": "Cannon furnace", "buy_average": 175920, "sell_average": 178136, "overall_average": 187000, "id": 12, "members": true, "sp": 187500}, "4099": {"name": "Mystic hat (dark)", "buy_average": 11350, "sell_average": 11754, "overall_average": 11956, "id": 4099, "members": true, "sp": 15000}, "28": {"name": "Insect repellent", "buy_average": 450, "sell_average": 707, "overall_average": 750, "id": 28, "members": true, "sp": 3}, "30": {"name": "Bucket of wax", "buy_average": 0, "sell_average": 350, "overall_average": 350, "id": 30, "members": true, "sp": 6}, "4101": {"name": "Mystic robe top (dark)", "buy_average": 71235, "sell_average": 71800, "overall_average": 73595, "id": 4101, "members": true, "sp": 120000}, "36": {"name": "Candle", "buy_average": 173, "sell_average": 215, "overall_average": 217, "id": 36, "members": true, "sp": 3}, "39": {"name": "Bronze arrowtips", "buy_average": 7, "sell_average": 9, "overall_average": 10, "id": 39, "members": true, "sp": 1}, "40": {"name": "Iron arrowtips", "buy_average": 4, "sell_average": 4, "overall_average": 5, "id": 40, "members": true, "sp": 2}, "41": {"name": "Steel arrowtips", "buy_average": 25, "sell_average": 27, "overall_average": 30, "id": 41, "members": true, "sp": 6}.......
完整档案:https://rsbuddy.com/exchange/summary.json
现在我的班级看起来像这样:
public class ItemSummary
{
public string name { get; set; }
public int buy_average { get; set; }
public int sell_average { get; set; }
public int overall_average { get; set; }
public int id { get; set; }
public bool members { get; set; }
public int sp { get; set; }
}
我的反序列化代码:
public string getitemsummaryraw()
{
apisource = "https://rsbuddy.com/exchange/summary.json";
try
{
webreq = WebRequest.Create(apisource);
webres = webreq.GetResponse();
webstream = webres.GetResponseStream();
using (sr = new StreamReader(webstream))
{
jsonstring = sr.ReadToEnd();
return jsonstring;
}
}
catch (WebException e)
{
Console.WriteLine(e);
return null;
}
}
public List<ItemSummary> getitemsummary()
{
string rawjson = getitemsummaryraw();
if (rawjson != null)
{
var itemlist = JsonConvert.DeserializeObject<Dictionary<string, List<ItemSummary>>>(rawjson);
var items = itemlist.SelectMany(entry => entry.Value);
return items.ToList<ItemSummary>();
}
else
{
return null;
}
}
我收到以下错误:
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[OSRS_Item_Logger.ItemSummary]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
有任何帮助吗? 我一直在努力解决这个问题,真的可以在这里使用一些帮助。
道歉,我写这篇文章的时间是早上4点。这根本不是功课。这是我在自己的时间里正在努力的事情。 我添加了json反序列化的其余代码。