c# - 如何解析包含对象列表的 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;
}
但是我不能通过其中两个来正确解析对象。那么对此有什么想法吗?希望,我的解释很清楚。
解决方案
我会使用第一个变体。您的解决方案不起作用,因为 json 的名称与您的类不同。你应该重命名:
public List<Picture> Pictures { get; set; }
至
public List<Picture> hits { get; set; }
我不确定 JsonConvert 是否可以与 . 一起使用Hits
,但如果可以,由于命名约定,您应该使用它。
编辑:如果您想继续使用图片作为属性名称,您可以使用 JsonProperty 属性,如下所示:
[JsonProperty("hits")]
public List<Picture> Pictures { get; set; }
推荐阅读
- python - 在 Mac 上安装 python 2.6
- d - 如何声明一组红黑树?
- python - FB Auto Group 海报与 Python 中的 Selenium
- android - Admob 插页式广告,onBackPressed 不会关闭广告
- mongodb - 如果未压缩的数据大小大于缓存大小,MongoDB 总是会出现校验和错误
- python - 将 2d 列表与列表理解而不是 for 循环连接
- html - flexbox div 中的绝对 div 表现得像固定的
- mockito - 我可以在 mockito 参数中混合一个匹配器和 1 个精确值吗
- jestjs - @quasar/extras/mdi-v4 开玩笑的单元测试在意外令牌“导出”上失败
- stored-procedures - EF CORE 调用 Oracle11g 的输出参数上的“数字或值错误”