【问题标题】:JSON Deserialization to get some particular objects and Serialize with new Json format using C#JSON 反序列化以获取某些特定对象并使用 C# 使用新的 Json 格式进行序列化
【发布时间】:2016-06-23 16:23:47
【问题描述】:

对于我的项目,我需要反序列化来自 Wikipedia 的长 JSON 数据。然后我需要从该 JSON 中获取一些特定信息,例如标题、提取、缩略图源、坐标。获得所有这些数据后,我需要以新格式的 JSON 再次对其进行序列化。因此我为此编写了一个代码。但是这段代码有很多问题。除了编码之外,我还描述了每个错误。

我正在使用来自维基百科的这个 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 对象。我想要我的最终 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; }
}

我正在使用此代码对 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)
    {
    }
}

【问题讨论】:

    标签: c# serialization json.net json-deserialization jsonserializer


    【解决方案1】:

    只需将序列化代码移出foreach 并序列化list&lt;poi&gt;

    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);
    

    【讨论】:

    • 你能再解释一下吗
    • 我添加了一个示例代码。这能回答你的问题吗?
    • Common.. 是不是就像这是我的一段代码,请帮我调试一下??真的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多