How can I asynchronously parse JSON data from a URL on a Windows phone and display it in a

As an Android developer exploring the world of Windows Phone for the first time, I came across this resource on how to handle list boxes in Windows Phone 7/8. However, my challenge now is parsing JSON from a URL instead of XML as shown in the example. While the provided code snippet was helpful for working with XML asynchronously in a list box, I am looking to achieve the same functionality but with JSON data.

Below is the portion of code I used to retrieve data from a URL:

private void FlickrSearch_Click(object sender, RoutedEventArgs e)
    { 
      WebClient webclient = new WebClient(); 
      webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted); 
      webclient.DownloadStringAsync(new Uri("http://api.flickr.com/services/feeds/photos_public.gne?tag=" + SearchBox.Text + "&format=rss2")); // Flickr search 
  }

If anyone could provide guidance on handling JSON from a URL in a Windows Phone application, it would be greatly appreciated.

Answer №1

It seems like the scope of this question is quite broad for a detailed answer. However, assuming that you have already successfully implemented XML data based on the guidelines provided in the blog post mentioned above, making adjustments to work with JSON should not be too difficult.

  1. Begin by Installing Json.Net from NuGet Package Manager
  2. Examine the JSON format returned by the API by visiting the API search URL in your browser. Make sure to change the format specification to JSON, for instance, . You will notice that the returned JSON string is not valid and may require some manual cleanup to rectify this issue: remove "jsonFlickrFeed(" at the beginning and ")" at the end of the JSON string. After making these adjustments, the string will be ready for deserialization using Json.Net.
  3. The remaining steps should align with the instructions provided in the original blog post

Below is a code snippet illustrating the process of handling JSON data:

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
   if (e.Error != null)
    { 
       MessageBox.Show("error");
       return;
    } 

    var jsonString = e.Result;

    //clean up the JSON string to ensure its validity
    jsonString = jsonString.Replace("jsonFlickrFeed(", "");
    jsonString = jsonString.Remove(jsonString.Length - 1);

    //deserialize the JSON string into an object
    var rootObject = JsonConvert.DeserializeObject<JObject>(jsonString);
    var items = (JArray)rootObject["items"];

    //populate a listbox with items extracted from the JSON
    listBox1.ItemsSource = from tweet in items                                     
    select new FlickrData                                   
     { 
       ImageSource = tweet["media"]["m"].ToString(), 
       Message = tweet["description"].ToString(), 
       UserName = tweet["title"].ToString(), 
       PubDate = DateTime.Parse(tweet["published"].ToString())                                    
     };                 
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for refreshing Facebook's og tags

I've been racking my brains over this issue for days, and despite the abundance of similar inquiries, I have yet to find a solution. On my blog-like website, each post requires its own title and description for Facebook articles. I know I can set the ...

Creating obstacles in a canvas can add an extra layer of challenges and

I am working on creating a basic platformer game using the code displayed below. window.onload = function(){ var canvas = document.getElementById('game'); var ctx = canvas.getContext("2d"); var rightKeyPress = false; var leftKeyPress = false; ...

Identifying the presence of node.js on your system

After installing node.js, I found myself at a loss on how to run applications. Despite the lack of instructions, I was determined to test if it was working by executing a script named hello.js: console.log('hello world'); I couldn't help b ...

The fuse-sidebar elements are not being properly highlighted by Introjs

I have recently developed an angular project that utilizes the fuse-sidebar component. Additionally, I am incorporating introjs into the project. While introjs is functioning properly, it does not highlight elements contained within the fuse-sidebar. The ...

changing the css transformation into zoom and positioning

My goal is to incorporate pinch zoom and panning using hardware-accelerated CSS properties like transform: translate3d() scale3d(). After completing the gesture, I reset the transform and switch to CSS zoom along with absolute positioning. This method ensu ...

Exploring and Parsing JSON Hierarchies in Python Using Pandas from a Web Address

I am currently attempting to parse multi-level JSON data using pandas in order to store it in a data-frame for further analysis or printing purposes. My primary objective is to comprehend how to extract data from each level of the given JSON structure. Be ...

Is it possible to add to JSON formatting?

Here is the JSON object I have: new Ajax.Request(url, { method: 'post', contentType: "application/x-www-form-urlencoded", parameters: { "javax.faces.ViewState": encodedViewState, "client-id": options._clientId, ...

What are the steps for encoding a value using jquery serialize?

I attempted to encode all values using the following code: encodeURIComponent($("#customer_details").serialize()); Unfortunately, it did not produce the desired results. Is there a method to retrieve all elements on a form and individually encode each v ...

Utilizing Angular 1.5 and ES6 to Enhance Dependency Injection

Hi there, I am currently exploring Angular and attempting to integrate ES6 into my workflow. I seem to be facing an issue with dependency injection where I cannot seem to get it working as expected. Here is a snippet from my index.js file: import ...

Executing an npm task from a JavaScript file in a parent/child process scenario

As someone who is still learning about child-process, I need some additional clarification on the topic. The Situation I am trying to find a way to execute one js file as a separate process from another js file. I want to pass a specific argument (a numb ...

How can Python be used to export hierarchical JSON data into an Excel xls file?

Looking to transfer data from Python to xlsx format. Currently have the data stored in JSON format. It doesn't matter what type of data is being exported out of Python. Here's an example JSON structure for a single article: { 'Word Coun ...

Unable to create a clickable button within a CSS3DObject using Three.js

How can I create an interactive button on a CSS3DObject within a cube made up of 6 sides? The button is located on the yellow side, but I'm facing issues with clicking on it. Whenever I attempt to click on the button, the event.target always points to ...

Issue with OpenLayers Icon not appearing on screen

I just finished creating a SpringBoot app using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and packaging it as an executable JAR file. Within this app, I have integrated OpenLayers 4 library to display a map with an Icon. Howeve ...

Steps for adjusting the status of an interface key to required or optional in a dynamic manner

Imagine a scenario where there is a predefined type: interface Test { foo: number; bar?: { name: string; }; } const obj: Test; // The property 'bar' in the object 'obj' is currently optional Now consider a situatio ...

"Customize your text alignment with TinyMCE's align

I'm in the process of updating from an outdated version of TinyMCE to the most recent release. In the old TinyMCE, if you inserted an image and aligned it to the left, the HTML generated looked like this: < img src="testing.jpg" align="left" > ...

Guide on Capturing Szimek/Signature_Pad using PHP: How to Save Javascript as PHP Variable?

While perusing through StackOverflow, I stumbled upon Szimek/Signature_Pad which allows for the capturing of electronic/digital signatures using Javascript. Even after conducting research, I still find myself puzzled on how to capture the DATA URI into a ...

Displaying a message when there are no results in Vue.js using transition-group and encountering the error message "children must be keyed

Utilizing vue.js, I crafted a small data filter tool that boasts sleek transitions for added flair. However, I encountered an issue with displaying a message when there are no results matching the current filters. Here's what I attempted: <transit ...

Node-archiver: A tool for dynamically compressing PDF files

I am currently working on a project that involves generating multiple PDF files using pdfkit. I have an array of users, and for each user, I create a report using the createTable() function. The function returns a Buffer, which is then passed to archiver t ...

A guide on incorporating dynamic information into JSON files with PHP

I am currently working on developing a checkbox tree where I require dynamic values for checkboxes. Below is my code snippet. Initially, I have static data in JSON format and now I need to retrieve dynamic data from a MySQL database. Despite trying vario ...

JavaScriptDeseializer : Unable to serialize the provided array

Working with my asp.net application, I need to send data back to the server. To do this, I serialize an object and send it over. On the server side, I attempt to de-serialize the object using the code below: [Serializable] public class PassData ...