为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 ListView 中反序列化 JSON C# XAML XAMARIN

如何在 ListView 中反序列化 JSON C# XAML XAMARIN

C#
森林海 2023-08-20 15:42:14
我正在尝试从 json 下载日期并将其放入列表视图中,但我不知道该怎么做。我使用邮递员从我的 YRL 下载这些信息:{“用户”:[{“电子邮件”:“ciccio@libero.it”,“昵称”:“Franco”,“图像”:“http://localhost/MyWebService/images/”},{“电子邮件”: "email@test.it","nickname":"nickname","image":"http://localhost/MyWebService/images/test_img.png"},{"email":"provoo@controllo.it", “昵称”:“farfallo”,“图像”:“http://localhost/MyWebService/images/”}]}现在要将这些信息下载到我的应用程序中,我创建了这些类:public class Users    {        [JsonProperty("users", NullValueHandling = NullValueHandling.Ignore)]        public User[] UsersUsers { get; set; }    }    public class User    {        [JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)]        public string email { get; set; }        [JsonProperty("nickname", NullValueHandling = NullValueHandling.Ignore)]        public string nickname { get; set; }        [JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]        public string password { get; set; }        [JsonProperty("image", NullValueHandling = NullValueHandling.Ignore)]        public Uri image { get; set; }    }在我的 XAML 页面上我编写了以下代码:<?xml version="1.0" encoding="UTF-8" ?><ContentPage    xmlns="http://xamarin.com/schemas/2014/forms"    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    x:Class="TestLoginURL.View.HomePage">    <ContentPage.Content>        <StackLayout>            <Label Text="HomePage"></Label>            <ListView x:Name="ListViewUsers" RowHeight="60">                  <ListView.ItemTemplate>                      <DataTemplate>                          <ViewCell>  但不幸的是,当我打开此页面时,出现此错误并且应用程序陷入崩溃:Newtonsoft.Json.JsonSerializationException无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[TestLoginURL.Model.Users]',因为该类型需要 JSON 数组(例如 [1, 2,3])以正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,不是像这样的集合类型)数组或列表),可以从 JSON 对象反序列化。还可以将 JsonObjectAttribute 添加到类型中以强制其从 JSON 对象反序列化。路径“用户”,第 1 行,位置 9。那么...谁能告诉我 JSON 反序列化的错误在哪里以及正确的代码是什么?谢谢
查看完整描述

1 回答

?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

您应该声明用户列表,检查以下内容:


public class User

{

    [JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)]

    public string email { get; set; }


    [JsonProperty("nickname", NullValueHandling = NullValueHandling.Ignore)]

    public string nickname { get; set; }


    [JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]

    public string password { get; set; }


    [JsonProperty("image", NullValueHandling = NullValueHandling.Ignore)]

    public sring image { get; set; }

}



public class YourUsersData

{

  [JsonProperty("users", NullValueHandling = NullValueHandling.Ignore)]

    public List<User> Users { get; set; }

}

然后在隐藏代码中:


 var usersList = JsonConvert.DeserializeObject<YourUsersData>(content);

 ListViewUsers.ItemsSource = YourUsersData.Users;


查看完整回答
反对 回复 2023-08-20
  • 1 回答
  • 0 关注
  • 61 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信