user5850934
user5850934

Reputation:

Json Serialization using Web Api Controller 2 in ASP.Net MVC

I am trying to serialize json data for a specitic location inforamtion like Name, ShortText, ImageUrl and Geocordinates. I created a PoiInfo.cs class inside the Model for json object.So far the Name, ShortText and Imageurl serialization working well. But I am having problem with the Geo-information data. My PoiInfo.cs looks like this-

public class PoiInfo
 {
    public string Name { get; set; }
    public string Shorttext { get; set; }
    public GeoCoordinates GeoCoordinates { get; set; }
    public List<string> Images { get; set; }
}

public class GeoCoordinates
{
    public double Longitude { get; set; }
    public double Latitude { get; set; }
}

Now in the controller I am trying to serialize in this way. I am giving shortly-

    public JsonResult<PoiInfo> Get(string id)
      {
            WebClient client = new WebClient();
            var TextResponse = //Api I used from wikipedia
            var ImageResponse = //Api I used from wikipedia
            var GeoResponse = //Api I used from wikipedia


            var TextResponseJson = JsonConvert.DeserializeObject<Rootobject>(TextResponse);
            var TextfirstKey = TextResponseJson.query.pages.First().Key;
            var TextResult = TextResponseJson.query.pages[TextfirstKey].extract;

            var ImgresponseJson = //similar as before

            var GeoResponseJson = JsonConvert.DeserializeObject<GeoRootobject>(GeoResponse);
            var firstKey = GeoResponseJson.query.pages.First().Key;
            var Latitude = GeoResponseJson.query.pages[firstKey].coordinates.First().lat;
            var Longitude = GeoResponseJson.query.pages[firstKey].coordinates.First().lon;

            var result = new PoiInfo();
            result.Shorttext = TextResult;
            result.Name = id;
            result.Images = new List<string> { ImageResult };
            result.GeoCoordinates = new List<string> {Double.Parse(Latitude+Longitude)}; // showing error double to string


            return Json(result);
    }


  }

I want to get my result in the following way-

 {
 "Name": "Burgtor",
 "Shorttext": "The Burgtor, built 1444 in late Gothic style, was the northern city gate of Hanseatic Lübeck....
 "GeoCoordinates": {
 "Longitude": 10.6912,
  "Latitude": 53.8738
  },
 "Images": [
 "8AB1DF99.jpg or //Image url"
  ]
 }

So far everything is ok except GeoCordinates. How can I correct my code.

Upvotes: 0

Views: 1713

Answers (2)

user5850934
user5850934

Reputation:

This is my modified answer

          result.GeoCoordinates = new GeoCoordinates
            {
                Latitude = Latitude;
                Longitude = Longitude
            };

Upvotes: 0

serhiyb
serhiyb

Reputation: 4833

//...
result.GeoCoordinates = new GeoCoordinates{ 
                              Latitude = Double.Parse(Latitude), 
                              Longitude = Double.Parse(Longitude)
                            }; 
///...

Upvotes: 1

Related Questions