【问题标题】:Display nested JSON data in ListView (Xamarin.Forms, C #)在 ListView 中显示嵌套的 JSON 数据(Xamarin.Forms,C#)
【发布时间】:2019-12-11 00:09:34
【问题描述】:

我有一个无法解决的问题,希望你能帮助我。

在我的 Xamarin.Forms 应用程序中,我正在下载我想在 ListView 中显示的 JSON 数据。

JSON 嵌套数据如下所示:

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

我对这些数据有这样的类:

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; }
}

这是我的 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;

这是我在 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>

此代码不显示任何内容(白屏)。 我想显示第一级嵌套的“名称”和第二级嵌套的“所有者姓氏”。 应用基于:https://www.codementor.io/lutaayahuzaifahidris/xamarin-forms-simple-list-of-items-with-offline-capability-cross-platform-app-btyq0bihv 对stackoverflow的其他响应不起作用:(

【问题讨论】:

    标签: c# .net xaml listview xamarin.forms


    【解决方案1】:

    首先,请在var tr = JsonConvert.DeserializeObject&lt;List&lt;Items&gt;&gt;(content);处添加断点检查是否可以从url获取json数据,如果可以从url获取结果。

    您为列表视图设置了两个ItemsSource(请设置一个)。

    &lt;ListView x:Name="myList" HasUnevenRows="true" ItemsSource="{Binding devices}"&gt;myList.ItemsSource = trends;

    我使用了你的代码(你没有提供url,我必须将json数据放到txt文件中,然后读取它)。 我可以在列表视图中得到结果。

    注意我设置了myList.ItemsSource = tr.devices;,如果你只设置myList.ItemsSource =trends,你不能在listview中绑定名字。

    而且我不能直接将对象反序列化到列表,它会得到一个异常, var tr = JsonConvert.DeserializeObject&lt;List&lt;Items&gt;&gt;(content);

    我可以用这条线作为 DeserializeObject。 var tr = JsonConvert.DeserializeObject&lt;Items&gt;(text);

    这是我的列表视图代码。

       <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>
    

    【讨论】:

      【解决方案2】:
      {
        "devices": [
          {
            "id": 2,
            "name": "device1",
            "owner": {
              "owner_id": 10,
              "owner_surname": "XYZ"
            }
          },
          {
            "id": 3,
            "name": "device3",
            "owner": {
              "owner_id": 12,
              "owner_surname": "XVB"
            }
          }
        ]
      }
      

      嵌套JSON是模型之间关系的结果,例如设备模型与所有者模型相关,因此调用设备模型将级联到相关模型。
      要访问相关模型的特定属性,请使用 dot(.) 运算符:

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

      这将给出 12... 因此访问更深的嵌套 json 将是 basemodel.property(A).property(B).property(c)...等

      【讨论】:

        猜你喜欢
        • 2021-10-08
        • 1970-01-01
        • 2021-12-30
        • 2017-01-21
        • 2021-10-19
        • 1970-01-01
        • 2015-12-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多