从 Wordpress rest API, unexpected { in [0].title 获取时出现反序列化错误

Getting a deserialing error when getting from the Wordpress rest API, unexpected { in [0].title

我们正在 Xamarin 中创建一个针对 Android 市场的应用程序。 我们需要从 Wordpress API 中提取对象列表以填充列表视图。

该代码似乎在代码的反序列化部分出错。 EventLW是应用程序前端的ListView。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Net.Http;
using Newtonsoft.Json;

namespace StudioDen.View
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Events : ContentPage
    {
        public Events()
        {
            InitializeComponent();

            GetProducts();

        }

        private async void GetProducts()
        {
            HttpClient client = new HttpClient();
            var response = await client.GetStringAsync("http://studioden.uk/wp-json/wp/v2/events/");

            var events = JsonConvert.DeserializeObject<List<Events>>(response);
            eventLV.ItemsSource = events;
        }
    }
}

Newtonsoft.Json.JsonReaderException Message=Unexpected character encountered while parsing value: {. Path '[0].title', line 1, position 341.

对这里出了什么问题有什么想法吗?我遵循了 youtube 教程,我认为这不是直接与代码有关的问题,而是与调用中的 Json 字符串有关的问题。

您正在反序列化响应 object 而不是响应内容。以下是您需要做的

private async void GetProducts()
{
    HttpClient client = new HttpClient();
    var response = await 
    client.GetAsync("http://studioden.uk/wpjson/wp/v2/events/");

     if (response.IsSuccessStatusCode)
     {
         var content = await response.Content.ReadAsStringAsync ();
         var events = JsonConvert.DeserializeObject<List<Events>>(content);
     }
     else
     {
         //Do stuff based on the status, was the content found, 
         //was there a server error etc.
     }
    ...
}

上面代码的解释

您发送请求并获得响应 object,其中包括 headers、状态代码、消息等。

你检查请求是否成功(如果不是你想处理错误的请求或服务器错误等)

然后您需要将响应内容读入一个字符串,然后反序列化该字符串值中的 json。在最终填充您的列表视图之前

你必须确定API ResponseJSON格式,否则,先把它变成JSON格式,然后所有问题就迎刃而解了。 可以拿对象的Content为例。

更具体地说,您必须确保您要 Deserialize 一个 string 它是 JSON 格式。

创建一个class来解析JSON数据给它 并且它需要对您获取的数据有定义

所以你得到的数据 class 应该是

public class Event
{
    public string id { get; set; }
    public string date { get; set; }
    public string date_gmt { get; set; }
    public guid guid { get; set; }
    public string modified { get; set; }
    public string modified_gmt { get; set; }
    public string slug { get; set; }
    public string status { get; set; }
    public string type { get; set; }
    public string link { get; set; }
    public title title { get; set; }
    public content content { get; set; }
    public string featured_media { get; set; }
    public string template { get; set; }
}

public class guid
{
    public string rendered { get; set; }
}

public class title
{
    public string rendered { get; set; }
}
public class content
{
    public string rendered { get; set; }
    public string @protected { get; set; } //@ to ignore the keyword protected
}

另外,将您的客户端代码更改为此

HttpClient client = new HttpClient();
    var response = await client.GetAsync("http://studioden.uk/wpjson/wp/v2/events/").ConfigureAwait(false);

    if (response.IsSuccessStatusCode)
    {
         var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

        var events = JsonConvert.DeserializeObject<List<Event>>(content);
    }

此处 class 事件用于将 JSON 数据映射到它,以便您稍后可以从列表中访问它

注意:你不应该在你的问题中公开端点 这就是我看到数据并为其编写 class

的方式