从goo.gl Analytics访问JSON响应

问题描述:

第一篇文章在这里。另外,我认为自己是一个非常非常低级的入门级c#/ asp.net/MSSQL开发人员,因此我的知识基础是冬季松鼠坚果库的大小。从goo.gl Analytics访问JSON响应

问题:无法从来自goo.gl analytics的JSON响应中提取多级别(可能不正确的术语)参数值。

我最近偶然发现了google提供的goo.gl URL shortner API,并且很喜欢它!这是简短的代码,我在http://www.jphellemons.nl/post/Google-URL-shortener-API-(googl)-C-sharp-class-C.aspx找到。

public static string Shorten(string url)   
{ 
    string key = "my_google_provided_API_key"; 
    string post = "{\"longUrl\": \"" + url + "\"}";    
    string shortUrl = url;    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + key);    
    try    
    {     
     request.ServicePoint.Expect100Continue = false;     
     request.Method = "POST";     
     request.ContentLength = post.Length;     
     request.ContentType = "application/json";     
     request.Headers.Add("Cache-Control", "no-cache");     
     using (Stream requestStream = request.GetRequestStream())     
     {      
      byte[] postBuffer = Encoding.ASCII.GetBytes(post);      
      requestStream.Write(postBuffer, 0, postBuffer.Length);     
     }     
     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())     
     {      
      using (Stream responseStream = response.GetResponseStream())      
      {       
       using (StreamReader responseReader = new StreamReader(responseStream))       
       {        
        string json = responseReader.ReadToEnd();        
        shortUrl = Regex.Match(json, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;       
       }      
      }     
     }    
    }    
    catch (Exception ex)    
    {     
     // if Google's URL Shortner is down...     
     System.Diagnostics.Debug.WriteLine(ex.Message);     
     System.Diagnostics.Debug.WriteLine(ex.StackTrace);    
    }    
    return shortUrl;   
} 

现在,goo.gl还提供分析,如创建时间,状态和点击的短网址数量。返回的JSON字符串的格式如下:

{ 
"kind": "urlshortener#url", 
"id": value, 
"longUrl": value, 
"status": value, 
"created": value, 
"analytics": {  
"allTime": {  
      "shortUrlClicks": value,  
      "longUrlClicks": value,  
      "referrers": 
      [   
      {   
       "count": value,   
       "id": value   
      }, ...  
      ],  
      "countries": [ ... ],  
      "browsers": [ ... ],  
      "platforms": [ ... ]  
      },  
      "month": { ... },  
      "week": { ... },  
      "day": { ... },  
      "twoHours": { ... } 
    } 
} 

现在我可以提取JSON响应,没有问题的参数值(ID,状态,longurl等)的第一级。问题出现在我想提取时,从“analytics”的“allTime”中说“shortUrlClicks”。我已经让谷歌的世界好几天了,而且还没有结果。此外,已经尝试过各种序列化器,但仍然没有。我一直在使用提取的ID,状态和longurl是:

protected void load_analytics(string shorturl) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?&shortUrl=" + shorturl+ "&projection=FULL"); 

     request.ServicePoint.Expect100Continue = false; 
     request.Method = WebRequestMethods.Http.Get; 
     request.Accept = "application/json"; 
     request.ContentType = "application/json; charset=utf-8"; 
     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
     { 
      using (Stream responseStream = response.GetResponseStream()) 
      { 
       using (StreamReader responseReader = new StreamReader(responseStream)) 
       { 
        String json = responseReader.ReadToEnd(); 
        String asdf = Regex.Match(json, @"""status"": ?""(?<status>.+)""").Groups["status"].Value; 
       } 
      } 
     } 
} 

发现的解决方案!!!!!! * 解决方案发现!!!!!! * 解决方案发现!!!!!!

谢谢你的回复。你绝对没有使用正则表达式。在与我的“古茹”进行了一些咨询后,我们发现了http://blogs.msdn.com/b/rakkimk/archive/2009/01/30/asp-net-json-serialization-and-deserialization.aspx

将溶液最终成为:

-create的类来表示JSON层次

public class Analytics 
{ 
    public Alltime alltime = new Alltime(); 
} 
public class Alltime 
{ 
    public int ShortUrlClicks; 
} 

- 下在响应流,反序列化到最高元件在层次结构中(分析类),瞧!

using (StreamReader responseReader = new StreamReader(responseStream)) 
       { 
        String json = responseReader.ReadToEnd(); 
        JavaScriptSerializer js = new JavaScriptSerializer(); 
        //String asdf = Regex.Match(json, @"""status"": ?""(?<status>.+)""").Groups["status"].Value; 
        Analytics p2 = js.Deserialize<Analytics>(json); 
        String fdas = p2.alltime.ShortUrlClicks.ToString(); 
        //note how i traverse through the classes where p2 is Analytics 
        //to alltime to ShortUrlClicks 
       } //fdas yields "0" which is correct since the shorturl I tested has never been clicked 

看看Json.NET库,不要使用正则表达式进行解析。