Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

Parse Google Calculator with Json in Windows Phone 7 / C#?

I'm currently making an application for WP7 where I'm implementing a currency exchange solution. I could make it with CSV too, but every time I find a snippet which looks alright (and modify it, etc.), I just meet limitations from the C# Silverlight libraries.

So basically, I'm now trying to filter out necessary information from the Google Calculator JSON result.

Basically this is the link: Google Calculator And this is the result of the JSON: {lhs: "10 U.S. dollars",rhs: "54.2090627 Danish kroner",error: "",icc: true}

Now, if I would like a textBlock to show "10 U.S. Dollars = 54.20 Danish Kroner", how would I have to parse and filter this? I would basically only need the application to go to the website on the click of a button, fetch the information, and return the result formated like above!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is actually pretty easy. I will illustrate making the call to the REST service and parsing the JSON data into a class. Then I think you'll be able to do the string concatenation and display on your own.

Start by adding a reference to the System.ServiceModel.Web assembly, which will give you access to the DataContractJsonSerializer in the System.Runtime.Serialization.Json namespace.

Next, create a class to represent the JSON. Use auto-implemented properties whose names match the JSON returned by the service:

public class ExchangeRate
{
  public string lhs { get; set; }
  public string rhs { get; set; }
  public string error { get; set; }
  public string icc { get; set; }
}

I'll assume you want to get the data when a button is clicked, so here's a small app with a button click handler.

using System;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Windows;
using Microsoft.Phone.Controls;

namespace WP7JsonClient
{
  public partial class MainPage : PhoneApplicationPage
  {
    public MainPage()
    {
      InitializeComponent();
    }

    private void button1_Click( object sender, RoutedEventArgs e )
    {
      var client = new WebClient();

      // Callback function written in-line as a lambda statement
      client.OpenReadCompleted +=
        ( s, eargs ) =>
        {
          var serializer = new DataContractJsonSerializer( typeof( ExchangeRate ) );
          var exchangeRate = (ExchangeRate)serializer.ReadObject( eargs.Result );

          // display exchange rate data here...
        };

      var uri = new Uri( "http://www.google.com/ig/calculator?hl=en&q=10USD=?DKK" );
      client.OpenReadAsync( uri );
    }
  }
}

I've written the async callback method in-line as a lambda statement, but you could just as easily write that as a separate method. After the call to have the serializer read the object, the JSON data is now available as an instance of your JSON serialization class (ExchangeRate) so you can work with that object directly, perform data-binding with its properties, and so on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...