将 JSON 反序列化为一个对象,然后 运行 一个 foreach 循环添加到一个列表视图中
Deserialize JSON into an object, and then run a foreach loop to add into a listview
我一直在努力反序列化来自我用于我的应用程序的 IGDB API 的 JSON 请求 return。我已经成功地在我的项目的另一部分反序列化,但是这个 JSON 有多个值,我不确定如何正确地转换成对象成员。我经常收到 System.Text.Json 个异常。
我的 GameListObject class 设置为:
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; }
}
Try 语句 API 请求和 return:
//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' 的变量进行操作,因为它不包含 public 的实例或扩展定义'GetEnumerator'
来自 API 的 return 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"
}
]
}
尝试使用 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; }
}
我一直在努力反序列化来自我用于我的应用程序的 IGDB API 的 JSON 请求 return。我已经成功地在我的项目的另一部分反序列化,但是这个 JSON 有多个值,我不确定如何正确地转换成对象成员。我经常收到 System.Text.Json 个异常。
我的 GameListObject class 设置为:
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; }
}
Try 语句 API 请求和 return:
//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' 的变量进行操作,因为它不包含 public 的实例或扩展定义'GetEnumerator'
来自 API 的 return 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"
}
]
}
尝试使用 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; }
}