c# - 在 UWP 中解析 Json

标签 c# json windows-phone win-universal-app

我正在研究 UWP,我正在尝试解析这个 Json,但不明白为什么这段代码不能工作,如果需要帮助,任何人都可以和我一起检查。谢谢。而且在获取列表变量(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 类型的值,您尝试将其转换为数组。

你可以试试这个。 首先从添加引用的管理 nuget 包中添加 Newtonsoft.Json。

enter image description here

现在将您的 json 响应粘贴到此站点 jsonToC#并将生成的类添加到您的项目中。对于您的引用 api,您将获得这样的 2 个类。

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; }
}

然后添加以下代码 & 将在结果中获取所有数据。

var json = await response.Content.ReadAsStringAsync();
var result= JsonConvert.DeserializeObject<RootObject>(json);

关于c# - 在 UWP 中解析 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36516146/

相关文章:

json - 在哪里添加 NPM 依赖项以运行安装后 Hook ?

html - 屏幕设计在 Windows Phone html5 应用程序的 Angular 导航中混合

windows-phone-8 - WinRT FlipView 类似 WP8 中的控件

windows-phone-7 - WebBrowserTask失败

c# - .aspx 文件中的 JavaScript 中的 "Expected expression"和 <%=foo%>

c# - .NET 6 Web API 中的 AddSerilog 和 UseSerilog 有什么区别?

c# - 如果字符串值不相等,如何更改 TextBlock 的背景颜色?

c# - Entity Framework 中的共享连接字符串

Python JSON 不打破循环

json - 将对象转换为可编码对象失败: 'Offset'的实例