我正在尝试将所有内容放在应用程序下方并将值放入Dictionary<string, Games>
我正在使用下面的代码。
班级:
public class Games
{
public int AppId { get; set; }
public string Name { get; set; }
}
代码:
public Dictionary<string, Games> GetAllGames()
{
//http://api.steampowered.com/ISteamApps/GetAppList/v0002/?key=STEAMKEY&format=json
var url = "http://api.steampowered.com/ISteamApps/GetAppList/v0002/?key=STEAMKEY&format=json";
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
var t = JObject.Parse(jsonString).SelectToken("applist").ToString();
var s = JObject.Parse(t).SelectToken("apps").ToString();
Dictionary<string, Games> gamesList = JsonConvert.DeserializeObject<Dictionary<string, Games>>(s);
return gamesList;
}
错误:
Newtonsoft.Json.JsonSerializationException
HResult=0x80131500
Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,LudumPricer.Games]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
JSON 示例:
{
"applist":{
"apps":[
{
"appid":5,
"name":"Dedicated Server"
},
{
"appid":7,
"name":"Steam Client"
},
{
"appid":8,
"name":"winui2"
},
{
"appid":10,
"name":"Counter-Strike"
}
]
}
}
我想获取apps
属性下的所有内容并将它们放入Dictionary <string, Games>
我正在使用 newtonsoft json.net 库