技术开发 频道

Windows phone如何实现json接口的调用

  解析JSON

  我们知道上述接口返回的数据是json格式的,为此我们需要通过C#对json类型的数据进行解析。为此我们在项目中添加System.ServiceModel.Web.dll的引用,以便使用DataContractJsonSerializer类。

解析JSON

  调用http://api.whatthetrend.com/api/v2/trends.json?api_key=1eb228f23d41dd3e8a042af5cf072ccfe41d0c22接口返回的json格式数据如下:

{
      
"api_version" : "2.0",
      
"as_of" : "2009-10-15 19:57:01",
      
"trends" : [
          {
"description" : { "created_at" : "2009-10-15 19:54:32",
                
"text" : "A 6 year old was believed to be stuck in a homemade hot air balloon in Denver, Colorado. The balloon landed with no boy found inside. The boy is still missing.",
                
"score" : "0"
              },
            
"first_trended_at" : "2009-10-15 19:00:01",
            
"last_trended_at" : "2009-10-15 19:55:01",
            
"name" : "Colorado",
            
"newly_trending" : "0",
            
"query" : "Colorado",
            
"trend_index" : "1",
            
"url" : "http://search.twitter.com/search?q=Colorado",
            
"category_id" : "11",
            
"category_name" : "News",
            
"locked" : false
          },
          {
"description" : { "created_at" : "2009-10-15 19:54:10",
                
"text" : "A 6 year old was believed to be stuck in a homemade hot air balloon in Denver, Colorado. The balloon landed with no boy found inside. The boy is still missing.",
                
"score" : "0"
              },
            
"first_trended_at" : "2009-10-15 19:30:01",
            
"last_trended_at" : "2009-10-15 19:55:01",
            
"name" : "#saveballoonboy",
            
"newly_trending" : "0",
            
"query" : "#saveballoonboy",
            
"trend_index" : "2",
            
"url" : "http://search.twitter.com/search?q=%23saveballoonboy",
            
"category_id" : "11",
            
"category_name" : "News",
            
"locked" : false
          }, ...
        ]
    }
    

  是的,这样的数据看起来是不是非常吓人,这里已经将一些关键的数据字段用红色字体标记出来。我们可以将这个json数据分为三部分

  api_version

  trends

  as_of

  而trends 是由如下属性构成的:

  name

  trend_id

  url

  category_id

  category_name

  query

  description

  first_trended_at

  last_trended_at

  newly_trending

  trend_index

  locked

  description是由如下属性构成的:

  created_at

  text

  score

  接下来我们需要使用C#构建这些数据的模式类,以便在程序中装配和使用数据。下面是这些类的代码。

/// <summary>
/// Model for twitter trend description
/// </summary>
public class TrendDescription
{
    
/// <summary>
    
/// Gets or sets the created_at.
    
/// </summary>
    
/// <value>The created_at.</value>
    
public DateTime created_at { get; set; }

    
/// <summary>
    
/// Gets or sets the text.
    
/// </summary>
    
/// <value>The text.</value>
    
public string text { get; set; }

    
/// <summary>
    
/// Gets or sets the score.
    
/// </summary>
    
/// <value>The score.</value>
    
public int score { get; set; }
}

/// <summary>
/// Model for twitter trend
/// </summary>
public class Trend
{
    
/// <summary>
    
/// Gets or sets the description.
    
/// </summary>
    
/// <value>The description.</value>
    
public TrendDescription description { get; set; }

    
/// <summary>
    
/// Gets or sets the first_trended_at.
    
/// </summary>
    
/// <value>The first_trended_at.</value>
    
public DateTime first_trended_at { get; set; }

    
/// <summary>
    
/// Gets or sets the last_trended_at.
    
/// </summary>
    
/// <value>The last_trended_at.</value>
    
public DateTime last_trended_at { get; set; }

    
/// <summary>
    
/// Gets or sets the name.
    
/// </summary>
    
/// <value>The name.</value>
    
public string name { get; set; }

    
/// <summary>
    
/// Gets or sets the newly_trending.
    
/// </summary>
    
/// <value>The newly_trending.</value>
    
public int newly_trending { get; set; }

    
/// <summary>
    
/// Gets or sets the query.
    
/// </summary>
    
/// <value>The query.</value>
    
public string query { get; set; }

    
/// <summary>
    
/// Gets or sets the trend_index.
    
/// </summary>
    
/// <value>The trend_index.</value>
    
public int trend_index { get; set; }

    
/// <summary>
    
/// Gets or sets the URL.
    
/// </summary>
    
/// <value>The URL.</value>
    
public string url { get; set; }

    
/// <summary>
    
/// Gets or sets the category_id.
    
/// </summary>
    
/// <value>The category_id.</value>
    
public int category_id { get; set; }

    
/// <summary>
    
/// Gets or sets the category_name.
    
/// </summary>
    
/// <value>The category_name.</value>
    
public string category_name { get; set; }

    
/// <summary>
    
/// Gets or sets a value indicating whether this <see cref="Trend"/> is locked.
    
/// </summary>
    
/// <value><c>true</c> if locked; otherwise, <c>false</c>.</value>
    
public bool locked { get; set; }
}

/// <summary>
/// Model for trends results
/// </summary>
public class TrendsResults
{
    
/// <summary>
    
/// Gets or sets the api_version.
    
/// </summary>
    
/// <value>The api_version.</value>
    
public string api_version { get; set; }

    
/// <summary>
    
/// Gets or sets the as_of.
    
/// </summary>
    
/// <value>The as_of.</value>
    
public DateTime as_of { get; set; }

    
/// <summary>
    
/// Gets or sets the trends.
    
/// </summary>
    
/// <value>The trends.</value>
    
public Trend[] trends { get; set; }
}

  现在我们就可以使用 DataContractJsonSerializer 来将json类型的数据装配到C#对象上。下面是实现解析的代码:

DataContractJsonSerializer dataContractJsonSerializer =
    
new DataContractJsonSerializer(typeof(TrendsResults));
TrendsResults trendsResults
=
    (TrendsResults)dataContractJsonSerializer.ReadObject(stream);

  上面代码中的stream是一个包含json数据的Stream对象。

0
相关文章