28 March 2017

Windows Store và Json: Pase Json sang String và Dialog từ Web App Windows Store C#

Bước 1: Tạo WebApplication và tạo file json có định dạng như sau:
Chạy Webapp ta có được như dưới
Bước 2: Tạo một Appliaction Windows Store 
MainPage.xaml
Windows Store 2017
<Page
    x:Class="DemoJSON.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DemoJSON"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="548,0,0,225" VerticalAlignment="Bottom" Click="button_Click"/>
        <TextBox x:Name="tb_firstName" HorizontalAlignment="Left" Margin="278,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="50" Width="347" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment"/>
        <TextBox x:Name="tb_lastName" HorizontalAlignment="Left" Margin="278,300,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="50" Width="347" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment"/>
        <TextBox x:Name="tb_age" HorizontalAlignment="Left" Height="55" Margin="278,410,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="347" d:LayoutOverrides="HorizontalAlignment"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="50" Margin="125,220,0,0" TextWrapping="Wrap" Text="FirstName" VerticalAlignment="Top" Width="105" d:LayoutOverrides="VerticalAlignment"/>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Height="50" Margin="125,330,0,0" TextWrapping="Wrap" Text="LastName" VerticalAlignment="Top" Width="105" d:LayoutOverrides="VerticalAlignment"/>
        <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Height="25" Margin="125,440,0,0" TextWrapping="Wrap" Text="Age" VerticalAlignment="Top" Width="105"/>

    </Grid>
</Page>
MainPage.xaml.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http;

namespace DemoJSON
{

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void button_Click(object sender, RoutedEventArgs e)
        {
            var client = new HttpClient();

            HttpResponseMessage response = await client.GetAsync(new Uri("http://localhost:58939/json.json"));
            var jsonstring = await response.Content.ReadAsStringAsync();
            
            //Show Json to Dialog
            var dialog = new MessageDialog(jsonstring);
            await dialog.ShowAsync();

            JsonValue jsonValue = JsonValue.Parse(jsonstring);

            //Pase json value to String + Set value cho TextBox
            string firstName = jsonValue.GetObject().GetNamedString("firstName");
            tb_firstName.Text = firstName;

            string LastName = jsonValue.GetObject().GetNamedString("lastName");
            tb_lastName.Text = LastName;

            double age = jsonValue.GetObject().GetNamedNumber("age");
            tb_age.Text = age + "";
 
        }

    }
}
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 23,
    "address": {
        "streetAddress": "Duy Tan",
        "city": "Ha Noi"
     },
    "phoneNumber": [
        {
          "type": "home",
          "number": "0789512854"
        }
    ]
}
Run test:
Show json to Dialog
 Set value cho TextBox

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang