对于我的项目,我需要对维基百科中的长JSON数据进行反序列化。然后我需要从JSON中获得一些特定的信息,如标题、提取、缩略图源、坐标。在得到所有这些数据后,我需要用一个新的格式化JSON再次序列化它。因此,我为此编写了一段代码。但是这个代码有很多问题。除了编码之外,我还描述了每一个错误。
我使用的是来自wikipedia 的这个api
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
这个json的C#对象如下-
public class Coordinate
{
public double lat { get; set; }
public double lon { get; set; }
public string primary { get; set; }
public string type { get; set; }
public string dim { get; set; }
public string globe { get; set; }
}
public class Thumbnail
{
public string source { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Page
{
public int pageid { get; set; }
public int ns { get; set; }
public string title { get; set; }
public string extract { get; set; }
public List<Coordinate> coordinates { get; set; }
public Thumbnail thumbnail { get; set; }
}
public class Query
{
public List<Page> pages { get; set; }
}
public class RootObject
{
public bool batchcomplete { get; set; }
public Query query { get; set; }
}
现在我创建了一个C#类来序列化我得到的Json对象。我想要我的最后一个儿子以这种方式-
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; }
}
我使用这段代码来反序列化和Seralize JSON对象。但是有很多问题,我在代码旁边提到过。
已编辑代码
using (WebClient client = new WebClient())
{
try
{
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<Poi> poi = new List<Poi>();
foreach (var page in json.query.pages)
{
Poi obj = new Poi();
obj.Title = page.title;
obj.Description =page.extract ;
var Image = new PoiImage();
var ImgfirstKey = page.thumbnail.source;
Image.ImageID = string.Format("{0:X}.jpg", ImgfirstKey.GetHashCode());
obj.Images = new List<PoiImage> {Image};
obj.Lat = page.coordinates.First().lat;
obj.Lon = page.coordinates.First().lon;
poi.Add(obj);
}
JsonSerializerSettings serializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented };
string result= Newtonsoft.Json.JsonConvert.SerializeObject(poi, serializerSettings);
Console.WriteLine(result);
}
catch(Exception)
{
}
}
只需将序列化代码移出foreach
并串行化list<poi>
foreach (page in rootObject.Query.Pages){
//do the magic
//then
poi.add(obj)
}
JsonSerializerSettings serializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented };
Newtonsoft.Json.JsonConvert.SerializeObject(poi, serializerSettings);