Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:46:36+00:00 2026-05-23T16:46:36+00:00

First post here. Also, I would consider myself a very, very low, entry-level c#/asp.net/MSSQL

  • 0

First post here. Also, I would consider myself a very, very low, entry-level c#/asp.net/MSSQL developer so my knowledge base is the size of a squirrels nut vault for the winter.

Problem: Can’t extract multi-level (may not be correct terminology) parameter values from JSON response from goo.gl analytics.

I recently stumbled across the goo.gl URL shortner API provided by google and love it! Here is the shortner code, which I found at 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;         
}

Now, goo.gl also provides analytics such as time created, status, and number of clicks on the short URL. The JSON string returned is in this format:

{  
"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": { ... }  
    }
}

Now I can extract the first level of parameter values (id, status, longurl, etc) from the JSON response, no problem. The issue comes in when I want to extract, say “shortUrlClicks” from “allTime” from “analytics”. I have scowered the world of google for days, and still no results. Also, have tried various serializers and still nothing. What I have been using to extract id, status, and longurl is:

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; 
                }
            }
        }
}

SOLUTION FOUND!!!!!!
*SOLUTION FOUND!!!!!!*
SOLUTION FOUND!!!!!!

Thank you for the reply. You are absolutely right in not using the regex. After a little consulting with my “guru,” we discovered http://blogs.msdn.com/b/rakkimk/archive/2009/01/30/asp-net-json-serialization-and-deserialization.aspx .

The solution ended up being :

-create the classes to represent the JSON hierarchy

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

-next in the response stream, deserialize to the highest element in the hierarchy (Analytics class), and voila!

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:46:37+00:00Added an answer on May 23, 2026 at 4:46 pm

    Look at Json.NET library, don’t use regex for parsing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post here on stackoverflow and am very impressed by the
First post here... I normally develop using PHP and Symfony with Propel and ActionScript
first post here, and probably an easy one. I've got the code from Processing's
first post here, I come in peace :) I've searched but can't quite find
My first post here, so i hope this is the right area. I am
This is my first post here and I wanted to get some input from
This is my first post here. I have a problem. I need to take
Well, this is my first post here and really enjoying the site. I have
First post, so here goes. I'm writing a script that does intelligent search and
This is my first time here so I hope I post this question at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.