I am just learning JSON implementation in Windows Phone 8 app
I got link of Earthquake News feeding, I want to use below link and fetch the all data to my Windows Phone app, I am completely noob in this, so please tell me how I can implement it in simple way in C# language.
Link: http://earthquake-report.com/feeds/recent-eq?json
Loading


Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 6:31 PM EDT
string s = Convert.ToString(e.Result);
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 18, 2014, 6:41 AM EDT
Can you try in this way, it just an example
http://stackoverflow.com/questions/18120092/how-to-parse-the-json-data-in-windows-phone-8
http://blogs.msdn.com/b/wsdevsol/archive/2013/09/04/how-to-store-json-data-in-windows-phone-8.aspx
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 7:22 AM EDT
Can you paste at least 2 items from a list of result json data what your getting at that point ?
And can you give a try like this
List
>(e.Result);
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 8:15 AM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 8:21 AM EDT
SORRY close with flower brace and try it once if not I have another idea lets c with that
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 8:33 AM EDT
}
Aditya PatilRank #2,485 · 565 reputationPosted Jul 18, 2014, 9:00 PM EDT
I am completely new to JSON and parsing of it.
Aditya PatilRank #2,485 · 565 reputationPosted Jul 19, 2014, 1:31 AM EDT
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
webClient.DownloadStringAsync(new Uri("http://earthquake-report.com/feeds/recent-eq?json"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
//Parse JSON result
var root1 = JsonConvert.DeserializeObject
this.DataContext = root1;
TaskListBox.ItemsSource = root1.title;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
public class Earthquake
{
public string title { get; set; }
public string magnitude { get; set; }
public string location { get; set; }
public string depth { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string date_time { get; set; }
public string link { get; set; }
}
}
What is wrong in Above code, I am getting below Exception.
Aditya PatilRank #2,485 · 565 reputationPosted Jul 19, 2014, 2:05 AM EDT
Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Cannot deserial ize the current JSON object (e.g. {"name":"value"}) into type 'System.Collection s.Generic.List`1[ApiPost.position]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or chang e the deserialized type so that it is a normal .NET type (e.g. not a primitive t ype like integer, not a collection type like an array or List
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 4:33 AM EDT
I changed few code and then tried but I am not getting how to bind the data to the ListBox, so help me.
List
>(e.Result);
jsondata = JsonConvert.DeserializeObject
I changed this code of line.
My Whole code looks like this
namespace Earthquake_Updates
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
webClient.DownloadStringAsync(new Uri("http://earthquake-report.com/feeds/recent-eq?json"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
//Parse JSON result
List
>(e.Result);
TaskListBox.ItemsSource = jsondata;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
public class Earthquake
{
public string title { get; set; }
public string magnitude { get; set; }
public string location { get; set; }
public string depth { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string date_time { get; set; }
public string link { get; set; }
}
}
I think jsondata getting whole data but how nto bind it with ListBox that don't know. So please solve that, and Check code too that I am right or not?
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 8:10 AM EDT
http://prntscr.com/45k8l5
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 8:17 AM EDT
Error 8 Cannot implicitly convert type 'Earthquake_Updates.MainPage.Earthquake' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?) C:\Users\Aditya\Desktop\Earthquake Updates\Earthquake Updates\Earthquake Updates\MainPage.xaml.cs 37 51 Earthquake Updates
for below code
if (!string.IsNullOrEmpty(e.Result))
{
//Parse JSON result
List
>(e.Result);
foreach(Earthquake q in jsondata)
{
TaskListBox.ItemsSource = new Earthquake(){title = q.title, magnitude = q.magnitude, location=q.location, depth=q.depth, latitude=q.latitude,longitude=q.longitude,date_time=q.date_time,link = q.link); //Error here
}
}
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 8:27 AM EDT
Screenshot: http://prntscr.com/45ke4t
Error Message,
Error 2 Cannot implicitly convert type 'Earthquake_Updates.Earthquake' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?) C:\Users\Aditya\Desktop\Earthquake Updates\Earthquake Updates\Earthquake Updates\MainPage.xaml.cs 37 51 Earthquake Updates
Error Screen: http://prntscr.com/45kekh
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 8:35 AM EDT
Error 2 The name 'Add' does not exist in the current context C:\Users\Aditya\Desktop\Earthquake Updates\Earthquake Updates\Earthquake Updates\MainPage.xaml.cs 37 72 Earthquake Updates
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 8:38 AM EDT
Please try to implement in this way
http://dotnet.dzone.com/news/how-bind-data-listbox-windows
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 8:41 AM EDT
it creates new stub of Add
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 3:43 PM EDT
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
webClient.DownloadStringAsync(new Uri("http://earthquake-report.com/feeds/recent-eq?json"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
//Parse JSON result
List
>(e.Result).ToList(); ;
foreach (Earthquake q in jsondata)
{
TaskListBox.ItemsSource = new Earthquakelist { Add(new Earthquake { title = q.title, magnitude = q.magnitude, location = q.location, depth = q.depth, latitude = q.latitude, longitude = q.longitude, date_time = q.date_time, link = q.link }) };
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
public class Earthquakelist : List
{ }
public class Earthquake
{
public string title { get; set; }
public string magnitude { get; set; }
public string location { get; set; }
public string depth { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string date_time { get; set; }
public string link { get; set; }
}
}
My Current code, Error still persist on Add,
As dzone link they added all items in
publicclassMovieList : List
Same way I tried to add all in public class Earthquakelist : List
{ }
Its shows error to jsondata
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 5:32 PM EDT
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 5:48 PM EDT
3257
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 8:06 PM EDT
Teamviewer details
593 940 517
6840
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 8:17 PM EDT
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 8:21 PM EDT
Please Join me
Teamviewer details
593 940 517
6840
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 9:28 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 23, 2014, 9:50 PM EDT
Aditya PatilRank #2,485 · 565 reputationPosted Jul 23, 2014, 9:52 PM EDT
Thanx @Ramesh Sir for your big help
Aditya PatilRank #2,485 · 565 reputationPosted Jul 24, 2014, 3:20 AM EDT
For parsing JSON content in the Windows Phone app, there is Reference Json.NET which we can add in the references so it may be easy and least number of steps to parse JSON.
Also I heard there is SimpleJson Reference id there nwhich also can help us to parse JSON easily, no need to create Serializer and MemoryStream.
So if you want to research go for Json.NET of Newtonsoft.JSON. may be helpful for us.
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 24, 2014, 5:02 AM EDT
I agree with you and I know about Json.Net before 6 months it self when I used to work with mongodb using c# yesterday I was using trail windows sdk that to 2010 so it's hard to install extended packages, I didn't find Nuget to do that anyways thanks once again it's good sharing knowledge with you and I have seen ur hard work and trouble as newbie keep going buddy all the best :)
Aditya PatilRank #2,485 · 565 reputationPosted Jul 24, 2014, 7:35 AM EDT
I Binded properly all data to the Listbox, and arranged in well mannner Atlast, the big data can be seen in the whole screen too,
I want more help now, getting stucked, When I will click on one data or value it contain one link too, I want to User should navigate to that link. How I can do that?
Aditya PatilRank #2,485 · 565 reputationPosted Jul 24, 2014, 7:45 AM EDT
webBrowserTask.Uri = new Uri(eop.link, UriKind.RelativeOrAbsolute);
webBrowserTask.Show();
What parameter should pass at eop.link. When I click on 1st Index of listbox, its link should open in WebBrowser, if click on 5th Data then link of 5th should get open.
Can anyone tell me the parameter so that it can take link of selected index. Please.
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 24, 2014, 4:58 PM EDT
Zia ur Rehman2 reputationPosted Mar 18, 2015, 6:33 AM EDT
http://nkishorchandra.blogspot.com/2012/10/parsing-json-in-windows-phone.html#comment-form