I am trying to Deserilize json data from wikipedia api. I want to get only the Image information and then serilize again for my own formatted JSON Data. But the problem in my code is that it shows only the last data from the list, Not the entire data.The foreach loop is not working.
My Code to Deserialize and serialize json object is look like this-
[SwaggerResponse(HttpStatusCode.OK, "A", typeof(PoiImageAnswer))]
[SwaggerResponse(HttpStatusCode.BadRequest, "Empty Answer", typeof(ModelStateDictionary))]
[SwaggerResponse(HttpStatusCode.InternalServerError, "No Response")]
[HttpPost]
[ActionName("Image")]
public IHttpActionResult Image([FromBody] PoiImageRequest request)
{
var result = new PoiImageAnswer {TransactionID = request.TransactionID};
using (WebClient client = new WebClient())
{
var response = client.DownloadString("https://en.wikipedia.org/w/api.php?format=json&action=query&redirects=1&generator=geosearch&prop=extracts|coordinates|pageimages&ggslimit=20&ggsradius=1000&ggscoord=52.5243700|13.4105300&&formatversion=2&exintro=1&explaintext=1&exlimit=20&coprop=type|dim|globe&colimit=20&piprop=thumbnail&pithumbsize=400&pilimit=20");
var json = JsonConvert.DeserializeObject<RootObject>(response);
List<PoiImageAnswer> poi = new List<PoiImageAnswer>();
foreach (var page in json.query.pages)
{
try
{
var Img= page.thumbnail.source;
result.Title = page.title;
result.Height = page.thumbnail.height;
result.Width = page.thumbnail.width;
result.ImageData = string.Format("{0:X}.jpg", page.thumbnail.source.GetHashCode());
result.TransactionID = request.TransactionID;
}
catch (Exception)
{
}
}
return Ok(result);
}
}
}
My C# class for json object is as below for understanding the code,
public class PoiImageAnswer
{
public string ImageID { set; get; }
public string Title { set; get; }
public int Width { set; get; }
public int Height { set; get; }
public string ImageData { set; get; }
public long TransactionID { set; get; }
}
public class Poi
{
public string Title { set; get; }
public string Description { set; get; }
public List<PoiImage> Images { set; get; }
public string OpeningHours { set; get; }
public double AirDistanceInKm { set; get; }
public double Lon { set; get; }
public double Lat { set; get; }
}
public class PoiImage
{
public string ImageID { set; get; }
}
I am getting output like this which is the last object from the list-
{
"ImageID": null,
"Title": "1976 Conference of Communist and Workers Parties of Europe",
"Width": 243,
"Height": 400,
"ImageData": "53E5DCF3.jpg",
"TransactionID": 0
}
You need code something like below. Note: I haven't tested it but it should work.
public IHttpActionResult Image([FromBody] PoiImageRequest request)
{
var resultList = new List<PoiImageAnswer>();
using (WebClient client = new WebClient())
{
var response = client.DownloadString("https://en.wikipedia.org/w/api.php?format=json&action=query&redirects=1&generator=geosearch&prop=extracts|coordinates|pageimages&ggslimit=20&ggsradius=1000&ggscoord=52.5243700|13.4105300&&formatversion=2&exintro=1&explaintext=1&exlimit=20&coprop=type|dim|globe&colimit=20&piprop=thumbnail&pithumbsize=400&pilimit=20");
var json = JsonConvert.DeserializeObject<RootObject>(response);
foreach (var page in json.query.pages)
{
try
{
var result = new PoiImageAnswer();
var Img = page.thumbnail.source;
result.Title = page.title;
result.Height = page.thumbnail.height;
result.Width = page.thumbnail.width;
result.ImageData = string.Format("{0:X}.jpg", page.thumbnail.source.GetHashCode());
result.TransactionID = request.TransactionID;
resultList.Add(result);
}
catch (Exception)
{
}
}
return Ok(resultList);
}
}