在Windows Phone 7/C#中使用Json解析Google计算器? [英] Parse Google Calculator with Json in Windows Phone 7 / C#?

查看:96
本文介绍了在Windows Phone 7/C#中使用Json解析Google计算器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为WP7制作一个应用程序,其中正在实现货币兑换解决方案.我也可以使用CSV来实现,但是每次我找到一个看起来不错的代码段(并对其进行修改等)时,我都遇到了C#Silverlight库中的限制.

因此,基本上,我现在正尝试从Google计算器JSON结果中过滤出必要的信息.

基本上这是链接: Google计算器 这是JSON的结果:{lhs: "10 U.S. dollars",rhs: "54.2090627 Danish kroner",error: "",icc: true}

现在,如果我想让textBlock显示"10美元= 54.20丹麦克朗",我该如何解析和过滤呢?我基本上只需要该应用程序,只需单击一个按钮即可访问网站,获取信息,并返回如上格式的结果!

解决方案

这实际上很容易.我将说明调用REST服务并将JSON数据解析为一个类.然后,我认为您可以进行字符串连接并自行显示.

首先添加对System.ServiceModel.Web程序集的引用,这将使您可以访问

我已经作为lambda语句在线编写了异步回调方法,但是您可以像单独的方法一样轻松地编写它.在调用让序列化程序读取对象之后,现在可以将JSON数据作为JSON序列化类(ExchangeRate)的实例使用,因此您可以直接使用该对象,使用其属性执行数据绑定,等等. /p>

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!

解决方案

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.

这篇关于在Windows Phone 7/C#中使用Json解析Google计算器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆