JSON API变量附加到多个元素

时间:2018-06-20 13:05:08

标签: c# json api xaml uwp

我有一个UWP项目,目前正在使用SQL数据库存储值和使用Json API从数据库将值获取到{{1 }}。

现在,我在达到值方面没有问题,也没有将值放入Visual studio变量中的问题。来自数据库的值是不同的房间,与之相关的是它们的所有属性和所有预订。

当我对房间的值进行反序列化时,我会这样做:

c#

这将毫无问题地获得房间的所有价值。然后在string url = "https://api.booking.com/api/company/a8fdbd0c-c0e4-470d-82b7-2b06a1a58dd8/rooms"; HttpClient client = new HttpClient(); string response = await client.GetStringAsync(url); List<Class2> data = JsonConvert.DeserializeObject<List<Class2>>(response); foreach (Class2 room in data) { } 循环中,我得到如下所有预订值:

foreach

在那部分下,我像上面一样制作一个新的string booking = $"https://api.booking.com/api/company/a8fdbd0c-c0e4-470d-82b7-2b06a1a58dd8/rooms/{room.id}/bookings"; HttpClient BookingClient = new HttpClient(); string BookingResponse = await BookingClient.GetStringAsync(booking); List<Bookings> bookings = JsonConvert.DeserializeObject<List<Bookings>>(BookingResponse); room.Bookings = bookings; foreach (var books in bookings) { } ,并使其在该foreach内进行:

foreach

我在这里想要实现的是,当变量if (books.timeFrom == Now) { ToRed(); } timeFrom相同时,有一种颜色需要更改。效果很好。

所以我的问题。...

比方说,我有一个房间是在09:00 AM预定的。然后我只希望在预订的那个特定房间改变颜色,但是现在每个房间都有颜色改变...

预订的DateTime.Now如下:

Json

您可以确定预订中包含一个名为{ "id": "a8fdbd0c-c0e4-470d-82b7-2b06a1a58dd8", "code": 4221, "timeFrom": "2018-06-14T09:00:00", "timeTo": "2018-06-14T10:00:00", "note": null, "createdDate": "2018-02-12T15:46:25.483", "room": { "name": "Rum 2", "id": "a8fdbd0c-c0e4-470d-82b7-2b06a1a58dd8", "seats": 5, "availableFrom": null, "availableTo": null, "roomAttributes": [ ] } } 的变量,因此这必须是使用该变量来确定要定位哪个房间的好方法,但是我已经尝试了很多,没有取得任何进展。 ..

编辑1:

我在room中有这样的房间模板:

UserControl

我从<UserControl> <Grid x:Name="MainGrid" Margin="0,0,0,0"> <TextBlock x:Name="CompanyName" Height="60" VerticalAlignment="Top" Text="{Binding RoomName}" FontSize="30" Margin="10,0,116,0" Width="257"/> <Rectangle x:Name="StatusColor" x:FieldModifier="public" Margin="0,0,0,0" Height="218" VerticalAlignment="Top"> </Rectangle> </Grid> </UserControl> 叫thet模板,像这样:

MainPage.xaml

编辑2:

这是预订<GridView x:Name="GridView1" ItemContainerStyle="{StaticResource testgrid}" ItemsSource="{x:Bind Rooms}" Width="1740" Height="835" IsHitTestVisible="False" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Margin="75,190,75,100" FontFamily="Segoe MDL2 Assets" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False"> <GridView.ItemTemplate> <DataTemplate x:DataType="data:Room"> <local:TemplateGrid x:Name="TemplateGrid"/> </DataTemplate> </GridView.ItemTemplate> </GridView> 中的内容:

Foreach

1 个答案:

答案 0 :(得分:1)

根据您的要求,您可以在var myString = File.ReadAllText(path) var myObject = JsonConvert.DeserializeObject<RootObject>(myString); 类中创建IsAvailable bool属性。并使用计时器来处理每个项目的RoomObject值,如下所示。

IsAvailable

MainPageViewModel

public class RoomObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string PropertyName = null)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }

    }

    public RoomObject(Item dataSourece)
    {
        this.dataSourece = dataSourece;
        ModifyAvilable();
    }

    private void ModifyAvilable()
    {
        this.Name = dataSourece.room.name;
        DispatcherTimer startTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(10) };
        startTimer.Tick += (s, e) =>
        {
            this.IsAvailable = true;
            DispatcherTimer endTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(10) };
            endTimer.Tick += (o, p) =>
            {
                this.IsAvailable = false;
                endTimer.Stop();
            };
            endTimer.Start();
            startTimer.Stop();
        };
        startTimer.Start();
    }

    public Item dataSourece;

    private bool _isAvailable;
    public bool IsAvailable
    {
        get
        {
            return _isAvailable;
        }
        set
        {
            _isAvailable = value;
            OnPropertyChanged();
        }
    }
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

}

Xaml使用情况

 public class MainPageViewModel
 {
     public MainPageViewModel()
     {
         GetJson();
     }
     public ObservableCollection<RoomObject> Items { get; set; } = new ObservableCollection<RoomObject>();
     private async void GetJson()
     {
         var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("json.txt");
         string json = await Windows.Storage.FileIO.ReadTextAsync(file);
         var item = JsonConvert.DeserializeObject<Item>(json);
         var roomitem = new RoomObject(item);
         Items.Add(roomitem);
     }  

 }

我已经上传了完整的代码示例,请参阅this