Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Wordpress rest API获取时发生反序列化错误,意外{in[0]。标题_C#_Xamarin_Xamarin.forms_Xamarin.android_Wordpress Rest Api - Fatal编程技术网

C# 从Wordpress rest API获取时发生反序列化错误,意外{in[0]。标题

C# 从Wordpress rest API获取时发生反序列化错误,意外{in[0]。标题,c#,xamarin,xamarin.forms,xamarin.android,wordpress-rest-api,C#,Xamarin,Xamarin.forms,Xamarin.android,Wordpress Rest Api,我们正在Xamarin中创建一个针对Android市场的应用程序。 为了填充列表视图,我们需要从WordPressAPI中提取一个对象列表 代码的反序列化部分似乎出错。 EventLW是应用程序前端的列表视图 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using X

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

代码的反序列化部分似乎出错。 EventLW是应用程序前端的列表视图

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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;
使用System.Net.Http;
使用Newtonsoft.Json;
名称空间StudioDen.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类事件:ContentPage
{
公共活动()
{
初始化组件();
GetProducts();
}
私有异步void GetProducts()
{
HttpClient=新的HttpClient();
var response=wait client.GetStringAsync(“http://studioden.uk/wp-json/wp/v2/events/");
var events=JsonConvert.DeserializeObject(响应);
eventLV.ItemsSource=事件;
}
}
}
Newtonsoft.Json.JsonReaderException Message=分析值时遇到意外字符:{.Path'[0].title',第1行,位置341


你知道这里出了什么问题吗?我学习了youtube教程,我认为这不是代码的问题,而是调用的Json字符串的问题。

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

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.
     }
    ...
}
private async void GetProducts()
{
HttpClient=新的HttpClient();
var响应=等待
client.GetAsync(“http://studioden.uk/wpjson/wp/v2/events/");
if(响应。IsSuccessStatusCode)
{
var content=await response.content.ReadAsStringAsync();
var events=JsonConvert.DeserializeObject(内容);
}
其他的
{
//根据状态做一些事情,是否找到了内容,
//是否存在服务器错误等。
}
...
}
对上述代码的解释

您发送一个请求并获得一个响应对象,该对象包括标题、状态代码、消息等

检查请求是否成功(如果不成功,则希望处理错误请求或服务器错误等)


然后,您需要将响应内容读入字符串,然后反序列化该字符串值中的json。在最终填充列表视图之前,您正在反序列化响应对象,而不是响应内容。以下是您需要执行的操作

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.
     }
    ...
}
private async void GetProducts()
{
HttpClient=新的HttpClient();
var响应=等待
client.GetAsync(“http://studioden.uk/wpjson/wp/v2/events/");
if(响应。IsSuccessStatusCode)
{
var content=await response.content.ReadAsStringAsync();
var events=JsonConvert.DeserializeObject(内容);
}
其他的
{
//根据状态做一些事情,是否找到了内容,
//是否存在服务器错误等。
}
...
}
对上述代码的解释

您发送一个请求并获得一个响应对象,该对象包括标题、状态代码、消息等

检查请求是否成功(如果不成功,则希望处理错误请求或服务器错误等)


然后,您需要将响应内容读入字符串,然后反序列化该字符串值中的json。在最终填充列表视图之前,您必须确保
API响应的格式为
json
格式,否则,首先将其设置为
json
格式,然后所有问题都将得到解决。 您可以以对象的
内容为例


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

您必须确保
API响应
JSON
格式,否则,首先将其设置为
JSON
格式,然后所有问题都将得到解决。 您可以以对象的
内容为例


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

创建一个类来解析JSON数据 它需要为您获得的数据定义

因此,您获取的数据的这个类应该是

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);
    }
HttpClient=newhttpclient();
var response=wait client.GetAsync(“http://studioden.uk/wpjson/wp/v2/events/“”。配置等待(错误);
if(响应。IsSuccessStatusCode)
{
var content=await response.content.ReadAsStringAsync().ConfigureAwait(false);
var events=JsonConvert.DeserializeObject(内容);
}
这里,类事件用于将JSON数据映射到它,以便您以后可以从列表中访问它

注意:您不应该在问题中暴露端点
这就是我看到数据并为其编写类的方式

创建一个类来解析JSON数据 它需要为您获得的数据定义

因此,您获取的数据的这个类应该是

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);
    }
HttpClient=newhttpclient();
var response=wait client.GetAsync(“http://studioden.uk/wpjson/wp/v2/events/“”。配置等待(错误);
if(响应。IsSuccessStatusCode)
{
var content=await response.content.ReadAsStringAsync().ConfigureAwait(false);
var events=JsonConvert.DeserializeObject(内容);
}
这里,类事件用于将JSON数据映射到它,以便您以后可以从列表中访问它

注意:你应该