Wednesday, January 9, 2013

Windows Phone JSON Parsing Application Example

Make a project with name MyIP and copy the following layout to it at MainPage.xaml


<phone:PhoneApplicationPage
    x:Class="MyIP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    shell:SystemTray.IsVisible="True">
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
            <TextBlock x:Name="ApplicationTitle" Text="COMPILED EXPERIENCE" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="my ip" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentGrid" Grid.Row="1">
        <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
            <TextBlock Text="your ip is:" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
            <TextBlock x:Name="IPAddress" Style="{StaticResource PhoneTextLargeStyle}" Text="127.0.0.1" Grid.Row="1">
            <TextBlock.Foreground>
            <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
            </TextBlock.Foreground>
            </TextBlock>
        </Grid>
    </Grid>
   
</phone:PhoneApplicationPage>


copy following code to MainPage.xaml.cs

using System;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Windows;

namespace MyIP
{
    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();

            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var webClient = new WebClient();

            webClient.OpenReadCompleted += OnOpenReadCompleted;

            webClient.OpenReadAsync(new Uri("http://compiledexperience.com/windows-phone-7/my-ip", UriKind.Absolute));
        }

        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            var serializer = new DataContractJsonSerializer(typeof(IPResult));
            var ipResult = (IPResult)serializer.ReadObject(e.Result);

            IPAddress.Text = ipResult.IP;
        }
    }
}


now make a class with name IPResult.cs and copy following code to it 

using System;
using System.Runtime.Serialization;

namespace MyIP
{
    [DataContract]
    public class IPResult
    {
        [DataMember(Name = "ip")]
        public string IP
        {
            get; set;
        }
    }
}


after running successfully you will get the desired result :)

5 comments:

  1. Hello admin,

    I was try the following code with new calss


    using System;
    using System.Runtime.Serialization;

    namespace MyIP
    {
    [DataContract]
    public class IPResult
    {
    [DataMember(Name = "ip")]
    public string IP
    {
    get; set;
    }
    }
    }

    In that code, I was found the error in the following code.

    [DataContract] and [DataMember(Name = "ip")]

    Please help me admin. How can I fix this problem.

    Regards ,

    VoDa Kar

    ReplyDelete
    Replies
    1. Hi please send yours code at upadhyay.jitesh@gmail.com that i can see where is the problem!!

      Delete
    2. Hello admin, I had been send the email..

      Regards ,

      VoDa Kar

      Delete
    3. i checkedit and same code was running successfully, plzz check yours namespace and all that it should be in respective namespaces

      Delete
    4. It's Ok, Admin

      Thanks a lot of .. and Have a nice days. ^_^

      Delete