首页 > 解决方案 > 如何解析包含对象列表的 JSON

问题描述

我需要将 JSON 解析为包含对象列表的对象。所以有我的JSON

我为它创建了两个类。有他们

public class Picture
{
    public int id { get; set; }
    public string pageURL { get; set; }
    public string type { get; set; }
    public string tags { get; set; }
    public string previewURL { get; set; }
    public int previewWidth { get; set; }
    public int previewHeight { get; set; }
    public string webformatURL { get; set; }
    public int webformatWidth { get; set; }
    public int webformatHeight { get; set; }
    public string largeImageURL { get; set; }
    public int imageWidth { get; set; }
    public int imageHeight { get; set; }
    public int imageSize { get; set; }
    public int views { get; set; }
    public int downloads { get; set; }
    public int favorites { get; set; }
    public int likes { get; set; }
    public int comments { get; set; }
    public int user_id { get; set; }
    public string user { get; set; }
    public string userImageURL { get; set; }
}
public class PicturesByTopic
{
    public int total { get; set; }
    public int totalHits { get; set; }
    public List<Picture> Pictures { get; set; }
    public PicturesByTopic()
    {
        Pictures = new List<Picture>();
    }
}

我试图以两种方式解析它们:

static PicturesByTopic GetPicturesByTopic(Message message)
{
    var topic = message.Text;
    var url = $"https://pixabay.com/api/?key={PicturesAPIKEY}&q={topic}&image_type=photo&pretty=true";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    string response;
    using (StreamReader stream = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        response = stream.ReadToEnd();
    }

    PicturesByTopic picturesByTopic = JsonConvert.DeserializeObject<PicturesByTopic>(response);
    return picturesByTopic;
   
}

第二个变体:

static List<PicturesByTopic> GetPicturesByTopic(Message message)
{
    var topic = message.Text;
    var url = $"https://pixabay.com/api/?key={PicturesAPIKEY}&q={topic}&image_type=photo&pretty=true";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    string response;
    using (StreamReader stream = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        response = stream.ReadToEnd();
    }

   
    var wrappedText = @"{ ""Prop"": " + response + " }";

    var jsonData = JObject.Parse(response);

    List<PicturesByTopic> RetsProperties = new List<PicturesByTopic>();

    foreach (var prop in jsonData["Prop"])
    {
        RetsProperties.Add(new PicturesByTopic
        {
            total = JsonConvert.DeserializeObject<int>(prop.First.ToString()),
            totalHits = JsonConvert.DeserializeObject<int>(prop.First.ToString()),
            Pictures = JsonConvert.DeserializeObject<Picture[]>(prop.First.ToString())
        });
    };

    return RetsProperties;

    
}

但是我不能通过其中两个来正确解析对象。那么对此有什么想法吗?希望,我的解释很清楚。

标签: c#jsonparsing

解决方案


我会使用第一个变体。您的解决方案不起作用,因为 json 的名称与您的类不同。你应该重命名:

public List<Picture> Pictures { get; set; }

public List<Picture> hits { get; set; }

我不确定 JsonConvert 是否可以与 . 一起使用Hits,但如果可以,由于命名约定,您应该使用它。

编辑:如果您想继续使用图片作为属性名称,您可以使用 JsonProperty 属性,如下所示:

[JsonProperty("hits")]
public List<Picture> Pictures { get; set; }

推荐阅读