2011-07-13 18 views
1

最初の投稿はこちらまた、私は非常に、非常に低い、エントリレベルのC#/ asp.net/MSSQL開発者と考えていますので、私の知識ベースは冬のリスナットボールトのサイズです。goo.glからのJSONレスポンスへのアクセス

問題:goo.gl analyticsのJSON応答から複数のレベル(正しい用語ではない可能性があります)のパラメータ値を抽出できません。

私は最近Googleが提供するgoo.gl URLショートナー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はまた、作成された時、ステータス、短いURLのクリック数などの分析を提供します。返された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」を抽出したい場合に発生します。私は数日間Googleの世界を襲ったが、まだ結果はない。また、さまざまなシリアライザを試したが、まだ何もしていない。私は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が見つかりました。 、

は、(解析クラス)階層内の最上位要素にデシリアライズ、応答ストリームにJSON階層

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

-nextを表現するためのクラスを-create:

ソリューションは、ことになりましたとボイル! Json.NET図書館で

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 

答えて

1

見て、解析のための正規表現を使用しないでください。

関連する問題