Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I search near by palce using google api

https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&key=YOUR_API_KEY


I want only vicinity information from result in json format.

Please advise how can I filter result and display only vicinity(address) information in json format.



String sURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=";
sURL += objNearByServicesRequest.Latitude + "," + objNearByServicesRequest.Longitude;
sURL += "&radius=" + WebConfigurationManager.AppSettings["RadiusNearBySearch"] + "&types=" + objNearByServicesRequest.Types + "&keyword=" + objNearByServicesRequest.Keyword;
sURL += "&key=" + WebConfigurationManager.AppSettings["GoogleAPIKeyNearBySearch"];

String strreturn = String.Empty;


using (var client = new WebClient())
using (var stream = client.OpenRead(sURL))
using (var reader = new StreamReader(stream))
{
var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadToEnd());
Console.WriteLine((string)jObject["results"][0]["vicinity"]);
}



I want to display out out in json format for 2 field like below.
how can I write code for display out put in below format using C#.

[
{
"id": 1,
"vicinity": "Opp. Railway Station, Gabba Estate,, Gohar Baug, Mumbai",

},
{
"id": 2
"vicinity": "SH 15, Navjivan Colony, Mumbai",

}

]


What I have tried:

>https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&key=YOUR_API_KEY


I want only vicinity information from below result in json format.

Please advise how can I filter result and display only vicinity(address) information in json format.
Posted
Updated 22-Dec-16 19:34pm
v10
Comments
Richard MacCutchan 21-Dec-16 5:49am    
You just need to extract the fields you are interested in, and ignore the rest.

1 solution

The most likely issue is that the response is on multiple lines, but you're only reading the first line.

Either change your code to read the entire response, using ReadToEnd instead of ReadLine:
C#
using (var client = new WebClient())
using (var stream = client.OpenRead(sURL))
using (var reader = new StreamReader(stream))
{
    var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadToEnd());
    Console.WriteLine((string)jObject["results"][0]["vicinity"]);
}

Or use a JsonReader to load the data:
C#
using (var client = new WebClient())
using (var stream = client.OpenRead(sURL))
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
    var jObject = Newtonsoft.Json.Linq.JObject.Load(json);
    Console.WriteLine((string)jObject["results"][0]["vicinity"]);
}
 
Share this answer
 
Comments
piyushpiyush 23-Dec-16 1:20am    
I want to display out out in json format for 2 field like below.
how can I write code for display out put in below format using C#.

[
{
"id": 1,
"vicinity": "An ice sculpture",

},
{
"id": 2
"vicinity": "A blue mouse",

}

]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900