我一直在努力反序列化從我用于我的應用程式的 IGDB API 回傳的 JSON 請求。我已經成功地在我專案的另一部分成功反序列化,但是這個 JSON 有多個值,我不確定如何正確轉換為物件成員。我不斷收到 System.Text.Json 例外。
我的 GameListObject 類設定為:
public class GameListObject
{
[JsonPropertyName("id")]
public string GameID { get; set; }
[JsonPropertyName("name")]
public string GameName { get; set; }
[JsonPropertyName("release_dates")]
public string ReleaseDate { get; set; }
}
API 請求和回傳的 Try 陳述句:
//On search box content change
private async void gamehub_search_TextChanged(object sender, TextChangedEventArgs e)
{
ObservableCollection<GameListObject> dataList = new ObservableCollection<GameListObject>();
gamehub_list.ItemsSource = dataList;
dataList.Clear();
var SearchQuery = gamehub_search.Text;
try
{
// Construct the HttpClient and Uri
HttpClient httpClient = new HttpClient();
Uri uri = new Uri("https://api.igdb.com/v4/games");
httpClient.DefaultRequestHeaders.Add("Client-ID", App.GlobalClientidIGDB);
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " App.GlobalAccessIGDB);
//Debug.WriteLine("Request Headers: ");
// Construct the JSON to post
HttpStringContent content = new HttpStringContent($"search \"{SearchQuery}\"; fields name,release_dates.human;");
Debug.WriteLine("Request Contents: " content);
// Post the JSON and wait for a response
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
uri,
content);
// Make sure the post succeeded, and write out the response
httpResponseMessage.EnsureSuccessStatusCode();
var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();
Debug.WriteLine("Request Response: " httpResponseBody);
//Deserialise the return output into game id, game name and release date
GameListObject gamelistobject = JsonSerializer.Deserialize<GameListObject>(httpResponseBody);
foreach (GameListObject GameListObject in gamelistobject) //Receiving error under 'gamelistobject'
{
//Add new item to the ListView
GameListObject newitem = new GameListObject() { GameID = gamelistobject.GameID, GameName = gamelistobject.GameName, ReleaseDate = gamelistobject.ReleaseDate };
dataList.Add(newitem);
}
Debug.WriteLine($"id: {gamelistobject.GameID}");
Debug.WriteLine($"name: {gamelistobject.GameName}");
Debug.WriteLine($"release_dates: {gamelistobject.ReleaseDate}");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
注意:在 Foreach 陳述句中接收錯誤:在“gamelistobject”下,說明“foreach 陳述句不能對“GameHubs.GameListObject”型別的變數進行操作,因為它不包含“GetEnumerator”的公共實體或擴展定義
從 API 回傳 JSON 陳述句的示例:
{
"id": 277,
"name": "Battlefield 2",
"release_dates": [
{
"id": 52086,
"human": "Jun 21, 2005"
},
{
"id": 52087,
"human": "Jun 22, 2005"
},
{
"id": 52088,
"human": "Jun 24, 2005"
}
]
},
{
"id": 89571,
"name": "Battlefield V: Deluxe Edition",
"release_dates": [
{
"id": 157810,
"human": "Nov 16, 2018"
},
{
"id": 157811,
"human": "Nov 16, 2018"
},
{
"id": 157812,
"human": "Nov 16, 2018"
}
]
}
uj5u.com熱心網友回復:
嘗試使用 List 而不是 GameListObject,在這種情況下你不需要 foreach 回圈
//Deserialise the return output into game id, game name and release date
List<GameListObject> gamelistobjects = JsonSerializer.Deserialize<List<GameListObject>>(httpResponseBody);
ObservableCollection<GameListObject> dataList =
new ObservableCollection<GameListObject>(gamelistobjects);
甚至一行都可以
ObservableCollection<GameListObject> dataList = JsonConvert.DeserializeObject<ObservableCollection<GameListObject>>(json);
測驗
foreach (var item in dataList)
{
Debug.WriteLine($"id: {item.GameID}");
Debug.WriteLine($"name: {item.GameName}");
if(item.ReleaseDates!=null)
foreach (var date in item.ReleaseDates)
{
Debug.WriteLine($"releaseDate: {date.Human}");
}
}
輸出
d: 277
name: Battlefield 2
releaseDate: Jun 21, 2005
releaseDate: Jun 22, 2005
releaseDate: Jun 24, 2005
id: 89571
name: Battlefield V: Deluxe Edition
releaseDate: Nov 16, 2018
releaseDate: Nov 16, 2018
releaseDate: Nov 16, 2018
并嘗試這門課
public partial class GameListObject
{
[JsonProperty("id")]
public long GameID { get; set; }
[JsonProperty("name")]
public string GameName { get; set; }
[JsonProperty("release_dates")]
public ObservableCollection<ReleaseDate> ReleaseDates { get; set; }
}
public partial class ReleaseDate
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("human")]
public string Human { get; set; }
}
public partial class ReleaseDate
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("human")]
public string Human { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341576.html
下一篇:多次按鍵旋轉會導致不需要的角度
