【问题标题】:How to parse JSON which include list of objects如何解析包含对象列表的 JSON
【发布时间】:2020-07-29 22:37:41
【问题描述】:

我需要将 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# json parsing


    【解决方案1】:

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

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

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

    我不确定 JsonConvert 是否适用于 Hits,但如果适用,由于命名约定,您应该使用它。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2020-05-24
      相关资源
      最近更新 更多