Solved

C# .NET 3.5 - Reading Google Maps JSON response

Posted on 2014-01-17
4
943 Views
Last Modified: 2014-01-21
Hey guys,

Whats the simplest way to get the first "formatted address" from this web response http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=-37.81516,144.962078

I don't really want to use JSON.net or other additional libraries.

Thank you.
0
Comment
Question by:mhdi
  • 2
4 Comments
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 250 total points
ID: 39791234
If you don't want to use a third-party library (although I'm not sure why you wouldn't use JSON.NET, it's free) then you can either create a .NET class that matches the JSON and use the JavaScriptSerializer's Deserialize<T>() method or you could parse the JSON string yourself (although I wouldn't recommend that, seems like a lot of work).
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 39791584
You can use a regular expression as indicated below, but I think JSON.NET is a better solution.

var client = new System.Net.WebClient();

var json = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=-37.81516,144.962078");

var address = Regex.Match(json, @"(?s)""formatted_address""\s*:\s*""(.+?)""").Groups[1].Value;

Console.WriteLine(address);

// Outputs:
// 20-22 McKillop Street, Melbourne VIC 3000, Australia
//

Open in new window

0
 

Author Comment

by:mhdi
ID: 39791586
Cool,

I just finished writing my own Regex. But maybe yours is better?

if(json.Length>10)
{
    Match match = Regex.Match(json, "\"formatted_address\" : \"(.*)\",", RegexOptions.IgnoreCase);
    if (match.Success)
        return match.Groups[1].Value;

}

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 39791596
Your regex will work fine as long as there is one property per line and there are no \n in the address.   Use the single line switch (?s) to handle \n in the address, and (.*?) to handle multiple properties in a single line.

Since you have no control over the json provided by the API, you should try to tight the regex.  BTW, JSON.NET already handles all the json syntax nuances (like escaped chars in the address), for that reason I believe is a better option.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question