当从Wordpress rest API获取信息时出现反序列错误,意外{在[0]中。title

问题描述 投票:1回答:4

我们正在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消息=解析值{时遇到意外字符。路径'[0] .title',第1行,位置341。

enter image description here

关于这里出什么问题的任何想法?我遵循了youtube教程,我认为这与代码无关,但与调用中的Json字符串无关。

c# xamarin xamarin.forms xamarin.android wordpress-rest-api
4个回答
0
投票

[您必须确保API响应为JSON格式,否则,请先使其以JSON格式,然后将解决所有问题。您可以以对象的content为例。

更具体地说,您必须确保要反序列化JSON格式的字符串。


0
投票

在哪里定义ContentPage?如我所见,您正在尝试将json反序列化为事件列表(从ContentPage派生。)>

您能提供ContentPage定义吗?


0
投票

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


0
投票

创建一个类以将JSON数据解析为它并且需要为您获取的数据定义]

© www.soinside.com 2019 - 2024. All rights reserved.