Solved

C# .NET 3.5 - Reading Google Maps JSON response

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Update Status Bar.... 1 24
Data Saving(2) 2 25
Change data in datatable 8 15
How to Get total minutes between two hours 4 27
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 document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

16 Experts available now in Live!

Get 1:1 Help Now