JSON字符串中的Rootobject列表

时间:2016-08-29 17:47:47

标签: c# json xaml data-binding

我认为JSON只是一个根对象。

 var parkinfo = new List<RootObjectUtrechtCity>();
        RootObjectUtrechtCity utrechtCityInfo = await ParkUtrechtCityProxy.GetUtrechtCity();
        parkStationsUtrechtCity.ItemsSource = parkinfo;

我想把这些人放在一个列表中,并将其绑定到xaml。如何,代码看起来不错,但绑定是一个问题。

JSON是:https://stallingsnet.nl/api/1/parkingcount/utrecht

Thanx提前,我会在我的应用程序中为您提供信用。

代理

public class ParkUtrechtCityProxy
{
    public async static Task<RootObjectUtrechtCity> GetUtrechtCity()
    {
        var http = new HttpClient();
        var response = await http.GetAsync("https://stallingsnet.nl/api/1/parkingcount/utrecht");
        var result = await response.Content.ReadAsStringAsync();
        var data = JsonConvert.DeserializeObject<RootObjectUtrechtCity>(result);
        return data;
    }
}

public class RootObjectUtrechtCity
{
    public int totalPlaces { get; set; }
    public string time { get; set; }
    public int freePlaces { get; set; }
    public string facilityName { get; set; }
    public int occupiedPlaces { get; set; }
}

XAML

<ListView x:Name="parkStationsUtrechtCity"
              Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <RelativePanel>
                    <TextBlock Name="parkPlacesUtrecht"
                               Text="{Binding facilityName}"/>
                    <TextBlock Name="parkAvailableUtrecht"
                               Text="{Binding freePlaces}"
                               RelativePanel.Below="parkPlacesUtrecht"/>
                </RelativePanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

1 个答案:

答案 0 :(得分:0)

从C#POCO的角度来看,您的JSON有效负载如下所示:

<?php
$mysqli = mysqli_connect("localhost","name","pass","database");
$query = 'UPDATE  users SET  permissions =  "content" WHERE permissions = ""';
$n = $mysqli->query($query);
$query = "SELECT * from users";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC);
$subdominio = $row["name"];
$created_at = $row["created_at"];
?>

因此,如果public class RootObject { public int totalPlaces { get; set; } public string time { get; set; } public int freePlaces { get; set; } public string facilityName { get; set; } public int occupiedPlaces { get; set; } } 从源获取您的json提要并正确反序列化,则代码应如下所示:

ParkUtrechtCityProxy.GetUtrechtCity()

更新

相应地更新代理以确保JSON正确序列化

var parkinfo = new List<RootObjectUtrechtCity>();
parkinfo  = await ParkUtrechtCityProxy.GetUtrechtCity();
parkStationsUtrechtCity.ItemsSource = parkinfo;