我有一個 JSON 呼叫,這給我帶來了一個帶有四個陣列的物件。
HttpWebRequest apiRequest = WebRequest.Create(url) as HttpWebRequest;
string apiResponse = "";
using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
apiResponse = reader.ReadToEnd();
}
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(apiResponse);
var results = dict["results"];
然后我var dict用來存盤這四個陣列,稍后我必須var results選擇一個特定的陣列。在這種情況下,我想要的陣列是"results".
var 結果“輸出”:

問題是我想訪問每個陣列的資料,但我不知道該怎么做。
ArrayList list = (ArrayList)results;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < list.Count; i )
{
if (builder.Length > 0)
{
builder.Append(", ");
}
object current = list[i];
builder.Append(current);
}
LblTitle.Text = builder.ToString();
我在其他帖子中看到了這段代碼,我適應了我的代碼,但這只會System.Collections.Generic.Dictionary'2[System.String,System.Object],為每個陣列帶來這個。
string[] arrayresult = builder.ToString();
string[] arrayresult2 = builder.ToString();
對于 object 中的每個陣列,我想要這樣的東西
results。然后像這樣訪問每個陣列資料LblTitle.Text = arrayresult[1]; LblTitle2.Text = arrayresult2[1];
編輯:添加 JSON 示例
"{\"page\":1,\"results\":[{\"adult\":false,\"backdrop_path\":\"/7ABsaBkO1jA2psC8Hy4IDhkID4h.jpg\",\"genre_ids\":[28,12,14,878],\"id\":19995,\"original_language\":\"en\",\"original_title\":\"Avatar\",\"overview\":\"In the 22nd century, a paraplegic Marine is dispatched to the moon Pandora on a unique mission, but becomes torn between following orders and protecting an alien civilization.\",\"popularity\":1363.938,\"poster_path\":\"/jRXYjXNq0Cs2TcJjLkki24MLp7u.jpg\",\"release_date\":\"2009-12-10\",\"title\":\"Avatar\",\"video\":false,\"vote_average\":7.5,\"vote_count\":26216},{\"adult\":false,\"backdrop_path\":\"/198vrF8k7mfQ4FjDJsBmdQcaiyq.jpg\",\"genre_ids\":[878,28,12],\"id\":76600,\"original_language\":\"en\",\"original_title\":\"Avatar: The Way of Water\",\"overview\":\"Set more than a decade after the events of the first film, learn the story of the Sully family (Jake, Neytiri, and their kids), the trouble that follows them, the lengths they go to keep each other safe, the battles they fight to stay alive, and the tragedies they endure.\",\"popularity\":1000.602,\"poster_path\":\"/1yppMeTNQwDrzaUH4dRCx4mr8We.jpg\",\"release_date\":\"2022-12-14\",\"title\":\"Avatar: The Way of Water\",\"video\":false,\"vote_average\":0,\"vote_count\":0},{\"adult\":false,\"backdrop_path\":null,\"genre_ids\":[99],\"id\":287003,\"original_language\":\"en\",\"original_title\":\"Avatar: Scene Deconstruction\",\"overview\":\"The deconstruction of the Avatar scenes and sets\",\"popularity\":261.979,\"poster_path\":\"/uCreCQFReeF0RiIXkQypRYHwikx.jpg\",\"release_date\":\"2009-12-18\",\"title\":\"Avatar: Scene Deconstruction\",\"video\":false,\"vote_average\":9,\"vote_count\":3},{\"adult\":false,\"backdrop_path\":null,\"genre_ids\":[99],\"id\":111332,\"original_language\":\"en\",\"original_title\":\"Avatar: Creating the World of Pandora\",\"overview\":\"The Making-of James Cameron's Avatar. It shows interesting parts of the work on the set.\",\"popularity\":259.613,\"poster_path\":\"/d9oqcfeCyc3zmMal6eJbfj3gatc.jpg\",\"release_date\":\"2010-02-07\",\"title\":\"Avatar: Creating the World of Pandora\",\"video\":false,\"vote_average\":7,\"vote_count\":20}],\"total_pages\":3,\"total_results\":55}"
我將此 JSON 轉換為 Class,感謝您的提示。這是我現在的課:
public class Result
{
public bool adult { get; set; }
public string backdrop_path { get; set; }
public List<int> genre_ids { get; set; }
public int id { get; set; }
public string original_language { get; set; }
public string original_title { get; set; }
public string overview { get; set; }
public double popularity { get; set; }
public string poster_path { get; set; }
public string release_date { get; set; }
public string title { get; set; }
public bool video { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
}
public class Root
{
public int page { get; set; }
public List<Result> results { get; set; }
public int total_pages { get; set; }
public int total_results { get; set; }
}
uj5u.com熱心網友回復:
我建議您創建一個適當的 C# 類來將您的 JSON 反序列化為。我們看不到 JSON 的樣子,所以我只舉一個簡單的例子。假設這是您的 JSON:
{
"results": [
[
"foo",
"bar"
],
[
"baz",
"qux"
]
],
"someOtherField": "someValue"
}
如果您訪問https://json2csharp.com/,您可以將其轉換為 C# 類,如下所示:
public class Root
{
public List<List<string>> results { get; set; }
public string someOtherField { get; set; }
}
要反序列化和訪問資料,您只需執行以下操作(我使用 System.Text.Json 而不是 JavaScriptSerializer - 請參閱下面的評論):
var root = JsonSerializer.Deserialize<Root>(apiResponse);
// ...
List<string> listResult = root.results[0];
List<string> listResult2 = root.results[1];
// ...
LblTitle.Text = listResult[1]; // "bar"
LblTitle2.Text = listResult2[1]; // "qux"
請參考JavaScriptSerializer 的定義:
對于 .NET Framework 4.7.2 及更高版本,使用 System.Text.Json 命名空間中的 API 進行序列化和反序列化。對于早期版本的 .NET Framework,請使用 Newtonsoft.Json。
請參閱此處了解如何使用 System.Text.Json。
如果您想改用 Newtonsoft.Json,則應替換JsonSerializer.Deserialize為JsonConvert.DeserializeObject:
var root = JsonConvert.DeserializeObject<Root>(apiResponse);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/513297.html
標籤:C#数组网json网络
下一篇:函式未正確地將變數遞增1
