Solved

C# .NET 3.5 - Reading Google Maps JSON response

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

713 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