Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had data like this in database

{"city":"CAMBRIDGE","id":"xyz","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyz","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyz"}

{"city":"CAMBRIDGE","id":"xyo","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyo","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyo"}

i want to pass that data as json result from web api

What I have tried:

C#
public HttpResponseMessage GetLocationData(string Latitude, string Longitude, float Miles)
       {
           List<Location> locations = _dpDataLayerService.GetLocations(Latitude, Longitude, Miles);

           var locationData = string.Empty;
           foreach (var item in locations)
           {
               locationData += item.Json + ',';
           }
                     var result = new HttpResponseMessage { Content = new StringContent(locationData, System.Text.Encoding.UTF8, "application/json") };
           return result;


       }


i want to show that data in array, now it showing as it is.
Posted
Updated 8-Jul-16 5:53am

A JSON array of those objects would look like

"locations":[{"city":"CAMBRIDGE","id":"xyz","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyz","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyz"},{"city":"CAMBRIDGE","id":"xyo","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyo","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyo"}]

ref : JSON Syntax[^]
 
Share this answer
 
Comments
dhiraj mane 7-Jul-16 9:40am    
Thanks, can u tell me how to add that data into locations. currently my locations list structure is
public class Location
{
public string StoreId { get; set; }
public string Json { get; set; }
public float Distance { get; set; }
}
Garth J Lancaster 7-Jul-16 20:58pm    
well, even my JSON was wrong - if you use

{"locations":[{"city":"CAMBRIDGE","id":"xyz","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyz","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyz"},{"city":"CAMBRIDGE","id":"xyo","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyo","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyo"}]}

you can deserialise to


public class Location
{
public string city { get; set; }
public string id { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string phoneNumber { get; set; }
public string state { get; set; }
public string storeNumber { get; set; }
public string storeType { get; set; }
public string streetAddress { get; set; }
public string zip { get; set; }
public string storeName { get; set; }
}

public class RootObject
{
public List<location> locations { get; set; }
}

or the simpler JSON representation

[{"city":"CAMBRIDGE","id":"xyz","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyz","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyz"},{"city":"CAMBRIDGE","id":"xyo","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyo","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyo"}]

can serialise to

public class RootObject
{
public string city { get; set; }
public string id { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string phoneNumber { get; set; }
public string state { get; set; }
public string storeNumber { get; set; }
public string storeType { get; set; }
public string streetAddress { get; set; }
public string zip { get; set; }
public string storeName { get; set; }
}

but you have to do some manual work - which is why I prefer the List<locations> method .. is that what you were asking ?
Garth J Lancaster 7-Jul-16 21:47pm    
so given

public class Location
{
public string city { get; set; }
public string id { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string phoneNumber { get; set; }
public string state { get; set; }
public string storeNumber { get; set; }
public string storeType { get; set; }
public string streetAddress { get; set; }
public string zip { get; set; }
public string storeName { get; set; }
}

and {"city":"CAMBRIDGE","id":"xyz","latitude":44.36164,"longitude":-72.11383,"phoneNumber":"","state":"MA","storeNumber":"142","storeType":"xyz","streetAddress":"330 RIVER STREET","zip":"02139","storeName":"xyz"} in a string 'json' you can do

Location aLocation = JsonConvert.DeserializeObject<location>(json);
dhiraj mane 8-Jul-16 11:49am    
Thanks for Help. I use Deserialize technique for that
C#
public IEnumerable<locationmodel> GetLocationData(string Latitude, string Longitude, float Miles)
       {
           List<location> locations = _dpDataLayerService.GetLocations(Latitude, Longitude, Miles);

           List<locationmodel> locationModel = new List<locationmodel>();

           var locationData = string.Empty;
           foreach (var item in locations)
           {
               LocationModel _location = new LocationModel();
               _location.Json = JsonConvert.DeserializeObject<json>(item.Json);
               locationModel.Add(_location);
           }

           return locationModel;
       }
 
Share this answer
 
v2

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