Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# .NET 3.5 - Reading Google Maps JSON response

Posted on 2014-01-17
4
Medium Priority
?
1,093 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 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

971 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