we can parse a json document in windows phone applications easily and it is easy to implement and understand.
1) please make a new project with the name JsonParsingExample2 and copy following xaml layout to MainPage.xaml
<phone:PhoneApplicationPage
x:Class="JsonParsingExample2.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"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" VerticalContentAlignment="Center" VerticalAlignment="Top">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button x:Name="myButton" Grid.Row="0" Height="75" Content="Click to Call JSON service 2 " VerticalAlignment="Top" />
<ListBox Name="lstEmployee" Grid.Row="2" FontSize="24">
</ListBox>
</Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
2) now please copy the following code to yours MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Runtime.Serialization.Json;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using System.IO;
using System.Text;
namespace JsonParsingExample2
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
myButton.Click += new RoutedEventHandler(myButton_Click);
}
void myButton_Click(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("http://api.androidhive.info/contacts/"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
foreach (var contactdata in rootObject.contacts)
{
lstEmployee.Items.Add("<=>" + contactdata.address + contactdata.email + contactdata.gender + contactdata.id + contactdata.name + contactdata.phone.home + contactdata.phone.mobile + contactdata.phone.office + "<=>");
}
}
public class Phone
{
public string mobile { get; set; }
public string home { get; set; }
public string office { get; set; }
}
public class Contact
{
public string id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string address { get; set; }
public string gender { get; set; }
public Phone phone { get; set; }
}
public class RootObject
{
public List<Contact> contacts { get; set; }
}
}
}
3) you need to take care of the references. i used Newtonsoft.Json.dll in my example so you need to add it to yours reference.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Runtime.Serialization.Json;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using System.IO;
using System.Text;
these above are the used references in this demo example code!!
4) run code successfully on emulator and you can see the out put after clicking the "Click to Call JSON service 2" button
Note: You need to get some getters and setters which you can get with the help of
http://json2csharp.com/
put the url there and you will get the supporting classes
i used the link http://api.androidhive.info/contacts/ for the demo example
:)
for Newtonsoft.json.dll you can use the link http://www.4shared.com/file/qXtiES9U/NewtonsoftJson.html or you can search with the other links as well through google!!
hii jeetesh i need your help ,i have to post my registration data on server through json ?can u help me regarding this..
ReplyDeleteHi please share yours problem!!
Deleteyes i have to send these below data on to server and server gives an response like {"message:user created "} the message is also in json format.
Deletecan you please explain me how to access data from online database in mysql
Deletescript written in php
plz replz as soon as possible
please reply
string postData = "name= " + txtusername.Text + "&userId= "
ReplyDelete+ txtid.Text + "&password= " + txtpassword.Password
+ "&email= " + txtemail.Text + "&mobileNo= " + txtmobileno.Text
+ "&securityQuestion=" + question.SelectedItem.ToString()
+ "&answer= " + txtanwer.Text + "&GCMId= " + DeviceIDAsString
+ "&university= " + university.SelectedItem.ToString() + "&city= "
+ city.SelectedItem.ToString() + "&state= " + country.SelectedItem.ToString() + "&year= + year.SelectedItem.ToString();
i have to post these fields data.
please visit http://stackoverflow.com/questions/16949031/windows-phone-send-parameters-to-server
Deletehope will help you!!
i tried this but still some errors are occured.can i send you my code..?
Deletehi presently i am at my office soon i will contact you and will share my mail id
Deletethanks jitesh ..:)
DeleteHi Jitesh
ReplyDeleteActually i am new developer on Windows Phone. Your tutorial is Awesome so take it to try but unfortunately i got a problem. Do you mind to help me to solve it?
Thanks and Best Regard
it works but when i click on the button, it displays this kind of mistake "An exception of type 'System.Reflection.TargetInvocationException' occurred in System.ni.dll but was not handled in user code"
DeleteSocial media is a great way to communicate with people. It is also a great way to marketing for your product and services. Because if you use this way of marketing, your targeted audience can easily find your product and can know all the information about your product or services. Best Social Media Marketing
ReplyDelete