问题描述
我正在开发UWP,并且试图解析此Json,但无法理解为什么此代码无法正常工作,请任何人可以与我联系以获取帮助.谢谢你.而且获取列表变量(列表)存在问题.
I’m working on UWP and I’m trying to parse this Json but can't understand why this code can't work please anyone can check it with me need help. Thank you. And there is a problem in getting the list variable (List).
感谢您的帮助.
namespace testapi2
{
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
jsonCall();
}
public async void jsonCall()
{
List<Result> listResult = new List<Result>();
var client = new HttpClient();
HttpResponseMessage response =
await client.GetAsync(new Uri("http://api-public.guidebox.com/v1.43/Tunisia/rKgEWJbFg0kgEHrcGXPKhPDo0XtTafyC/movies/all/250/250"));
client.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
var array = await response.Content.ReadAsStringAsync();
JsonArray ja = JsonValue.Parse(array).GetArray();
for (uint i = 0; i < ja.Count; i++)
{
//Debug.WriteLine(i);
Result result = new Result();
int id = Convert.ToInt32(ja.GetObjectAt(i).GetNamedValue("id"));
string title = ja.GetObjectAt(i).GetNamedString("title");
int release_year = Convert.ToInt32(ja.GetObjectAt(i).GetNamedNumber("release_year"));
int themoviedb = Convert.ToInt32(ja.GetObjectAt(i).GetNamedString("themoviedb"));
string original_title = ja.GetObjectAt(i).GetNamedString("original_title");
// List<object> alternate_titles = ja.GetObjectAt(i).GetNamedArray("alternate_titles");
string imdb = ja.GetObjectAt(i).GetNamedString("imdb");
bool pre_order = ja.GetObjectAt(i).GetNamedBoolean("pre_order");
bool in_theaters = ja.GetObjectAt(i).GetNamedBoolean("in_theaters");
string release_date = ja.GetObjectAt(i).GetNamedString("release_date");
string rating = ja.GetObjectAt(i).GetNamedString("rating");
int rottentomatoes = Convert.ToInt32(ja.GetObjectAt(i).GetNamedString("rottentomatoes"));
string freebase = ja.GetObjectAt(i).GetNamedString("freebase");
int wikipedia_id = Convert.ToInt32(ja.GetObjectAt(i).GetNamedString("wikipedia_id"));
string metacritic = ja.GetObjectAt(i).GetNamedString("metacritic");
string common_sense_media = ja.GetObjectAt(i).GetNamedString("common_sense_media");
string poster_120x171 = ja.GetObjectAt(i).GetNamedString("poster_120x171");
string poster_240x342 = ja.GetObjectAt(i).GetNamedString("poster_240x342");
string poster_400x570 = ja.GetObjectAt(i).GetNamedString("poster_400x570");
listResult.Add(result);
}
// Debug.WriteLine("hello", listResult);
list.ItemsSource = listResult;
}
}
}
推荐答案
您的api返回jsonObject类型的值,然后尝试将其转换为数组.
Your api return a value of jsonObject type and you try to convert that to an array.
您可以尝试一下.首先从添加引用的manage nuget包中添加Newtonsoft.Json.
you can try this.At first add Newtonsoft.Json from manage nuget package of add reference.
现在将您的json响应粘贴到此站点 jsonToC#并将生成的类添加到您的项目中.作为参考api,您将获得2个这样的类.
Now paste your json response to this site jsonToC# and add generated to classes to your project. For your reference api you will get 2 class like this.
public class Result
{
public int id { get; set; }
public string title { get; set; }
public int release_year { get; set; }
public int themoviedb { get; set; }
public string original_title { get; set; }
public List<object> alternate_titles { get; set; }
public string imdb { get; set; }
public bool pre_order { get; set; }
public bool in_theaters { get; set; }
public string release_date { get; set; }
public string rating { get; set; }
public int rottentomatoes { get; set; }
public string freebase { get; set; }
public int wikipedia_id { get; set; }
public string metacritic { get; set; }
public string common_sense_media { get; set; }
public string poster_120x171 { get; set; }
public string poster_240x342 { get; set; }
public string poster_400x570 { get; set; }
}
public class RootObject
{
public int total_results { get; set; }
public int total_returned { get; set; }
public List<Result> results { get; set; }
}
然后添加以下代码&将获得所有数据.
then add following code & will get all data at result.
var json = await response.Content.ReadAsStringAsync();
var result= JsonConvert.DeserializeObject<RootObject>(json);
这篇关于在UWP中解析Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!