0

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.

2 Answers 2

1
//...
result.GeoCoordinates = new GeoCoordinates{ 
                              Latitude = Double.Parse(Latitude), 
                              Longitude = Double.Parse(Longitude)
                            }; 
///...
Sign up to request clarification or add additional context in comments.

4 Comments

@JamieD77 Since C# 3.0 constructor parentheses are optional when using object initialiser .
If the comment you have next to geocode line is the error you're getting then the error is telling you what the problem is. You are trying to place a double into a list of strings. You need to cast the double to string to make it work.
@serhiyb: Thanks it is working. I need to add ToString() after Latidute and longitude.
@JamieD77 oh, makes sense. Thanks for your comment.
0

This is my modified answer

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

5 Comments

If Latitude and Longitude are not strings initially then what type are they?
did you try just taking off the Double.Parse instead?
When I tried with your code it shows error that - The best overloaded method match for 'double.Parse(string)' has some invalid arguments and another error Argument 1: cannot convert from 'double' to 'string'. I used this api to get geo information en.wikipedia.org/w/…
if Latitude is already a double, then you dont need to parse it in the first place.. did you try setting Latitude = Latitude and Longitude = Longitude without using double.parse?
@JamieD77 Thank you for your information. It is also working in this way if I set Latitude = Latitude and Longitude = Longitude.