简体   繁体   English

在 ListView (Xamarin.Forms, C#) 中显示嵌套的 JSON 数据

[英]Display nested JSON data in ListView (Xamarin.Forms, C #)

I have a problem that I can not solve, I hope you help me.我有一个我无法解决的问题,我希望你能帮助我。

In my Xamarin.Forms application I am downloading JSON data which I would like to display in ListView.在我的 Xamarin.Forms 应用程序中,我正在下载我想在 ListView 中显示的 JSON 数据。

JSON nasted data looks like this: JSON 嵌套数据如下所示:

{
  "devices": [
    {
      "id": 2,
      "name": "device1",
      "owner": {
        "owner_id": 10,
        "owner_surname": "XYZ"
      }
    },
    {
      "id": 3,
      "name": "device3",
      "owner": {
        "owner_id": 12,
        "owner_surname": "XVB"
      }
    }
  ]
}

I have such classes for these data:我有这些数据的类:

public class Items
{
    public List<Device> devices { get; set; }
}
public class Device
{
    public int id { get; set; }
    public string name { get; set; }
    public Owner owner { get; set; }
}
public class Owner
{
    public int owner_id { get; set; }
    public string owner_surname { get; set; }
}

This is my MainPage.xaml.cs file:这是我的 MainPage.xaml.cs 文件:

var client = new WebClient();
client2.Headers.Add("Accept", "application/json");
var content = client.DownloadString(Url);
var tr = JsonConvert.DeserializeObject<List<Items>>(content);
ObservableCollection<Items> trends = new ObservableCollection<Items>(tr);
myList.ItemsSource = trends;

And this is my ListView in XAML:这是我在 XAML 中的 ListView:

<ListView x:Name="myList" HasUnevenRows="true" ItemsSource="{Binding devices}">
    <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding name}" TextColor="Black" FontAttributes="Bold"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

This code does not display anything (white screen).此代码不显示任何内容(白屏)。 I would like to display the "name" from the first level of nesting and the "owner_surname" from the second level of nesting.我想显示来自第一级嵌套的“名称”和来自第二级嵌套的“owner_surname”。 application built on the basis of: https://www.codementor.io/lutaayahuzaifahidris/xamarin-forms-simple-list-of-items-with-offline-capability-cross-platform-app-btyq0bihv Other responses to stackoverflow did not work :(建立在以下基础上的应用程序: https : //www.codementor.io/lutaayahuzaifahidis/xamarin-forms-simple-list-of-items-with-offline-capability-cross-platform-app-btyq0bihv其他对 stackoverflow 的响应没有工作 :(

First of all, please add a breakpoint to var tr = JsonConvert.DeserializeObject<List<Items>>(content);首先请给var tr = JsonConvert.DeserializeObject<List<Items>>(content);添加断点check you can get the json data from the url, If you can get the result from the Url.检查是否可以从 url 获取 json 数据,如果可以从 Url 获取结果。

You set the two ItemsSource for the listview(please set one of them).您为列表视图设置了两个ItemsSource (请设置其中之一)。

<ListView x:Name="myList" HasUnevenRows="true" ItemsSource="{Binding devices}"> and myList.ItemsSource = trends; <ListView x:Name="myList" HasUnevenRows="true" ItemsSource="{Binding devices}">myList.ItemsSource = trends;

I used your code(You did not provide url, I have to put the json data to the txt file, then read it).我用了你的代码(你没有提供url,我必须把json数据放到txt文件中,然后读取它)。 I can get the result in the listview.我可以在列表视图中得到结果。

在此处输入图片说明

Notice I set the myList.ItemsSource = tr.devices;注意我设置了myList.ItemsSource = tr.devices; If you just set the myList.ItemsSource =trends , You can not bind the name in the listview.如果只是设置myList.ItemsSource =trends ,则无法在列表视图中绑定名称。

And I cannot DeserializeObject to List directly, it will get an exception, var tr = JsonConvert.DeserializeObject<List<Items>>(content);而且我不能直接将 DeserializeObject 到 List,它会得到一个异常, var tr = JsonConvert.DeserializeObject<List<Items>>(content);

I just could be DeserializeObject with this line.我只是可以用这一行来反序列化对象。 var tr = JsonConvert.DeserializeObject<Items>(text);

There is my listview code.有我的列表视图代码。

   <StackLayout>
    <!-- Place new controls here -->
    <ListView x:Name="myList" HasUnevenRows="true" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding name}" TextColor="Black" FontAttributes="Bold"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>
{
  "devices": [
    {
      "id": 2,
      "name": "device1",
      "owner": {
        "owner_id": 10,
        "owner_surname": "XYZ"
      }
    },
    {
      "id": 3,
      "name": "device3",
      "owner": {
        "owner_id": 12,
        "owner_surname": "XVB"
      }
    }
  ]
}

Nested JSON is as a result of relationship among models eg device model is related to owner model, thus calling device model will cascade to the related models.嵌套 JSON 是模型之间关系的结果,例如设备模型与所有者模型相关,因此调用设备模型将级联到相关模型。
To access specific properties of related models use dot(.) operator:要访问相关模型的特定属性,请使用 dot(.) 运算符:

<label Text="{Binding Owner.owner_id}"/> 

this will give 12... hence accessing even a deeper nested json would be basemodel.property(A).property(B).property(c) ...etc这将给 12 ... 因此访问更深的嵌套 json 将是basemodel.property(A).property(B).property(c) ...等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM