Solved

C# .NET 3.5 - Reading Google Maps JSON response

Posted on 2014-01-17
4
914 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now