7", "description-6ren">
gpt4 book ai didi

c# - 使用 LINQ 将 JSON 解析为对象列表

转载 作者:行者123 更新时间:2023-11-30 16:40:36 25 4
gpt4 key购买 nike

我对解析以下 JSON 结构的最佳方法有点困惑。

{
"featured": {
"id": 15,
"title": "media 1 -> 7",
"description": "test1",
"short_description": "test1",
"rating_avg": 0.0,
"image": "//d25xdrj7gd7wz1.cloudfront.net/covers/1603/1452024324.jpg"
},
"categories": [
{
"id": 1,
"title": "category 0",
"description": null,
"position": 0,
"media": [
{
"id": 1,
"title": "media 0 -> 0",
"description": "test1",
"short_description": "test1",
"rating_avg": 0.0,
"image": "//d25xdrj7gd7wz1.cloudfront.net/covers/1603/1452024324.jpg",
"category_media": {
"position": 0,
"category_id": 1,
"media_id": 1,
"id": 1
}
}, ...

基本上我有一组类别,其中包含一组媒体(特色是其他东西)

我希望返回 List 并且 Category 对象包含一个 List

我创建了一些模型:

public class Category
{
public string Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }

public List<Media> MediaList { get; set; }
}

public class Media
{
public string Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string ShortDescription { get; set; }
public string Image { get; set; }
}

..我应该使用 Newtonsoft?

我查看了以下示例:Deserializing Partial JSON Fragments但我认为我不需要从 JToken -> Category ... 等进行转换。换句话说,我认为只返回我的 List 会很容易。

我是 LINQ 的新手(我有 Python 背景)所以我开始了解 C#

最佳答案

以此为模型

using System;
using System.Collections.Generic;

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public partial class JsonModel
{
[JsonProperty("featured")]
public Featured Featured { get; set; }

[JsonProperty("categories")]
public List<Category> Categories { get; set; }
}

public partial class Category
{
[JsonProperty("id")]
public long Id { get; set; }

[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("description")]
public object Description { get; set; }

[JsonProperty("position")]
public long Position { get; set; }

[JsonProperty("media")]
public List<Featured> Media { get; set; }
}

public partial class Featured
{
[JsonProperty("id")]
public long Id { get; set; }

[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

[JsonProperty("short_description")]
public string ShortDescription { get; set; }

[JsonProperty("rating_avg")]
public long RatingAvg { get; set; }

[JsonProperty("image")]
public string Image { get; set; }

[JsonProperty("category_media", NullValueHandling = NullValueHandling.Ignore)]
public CategoryMedia CategoryMedia { get; set; }
}

public partial class CategoryMedia
{
[JsonProperty("position")]
public long Position { get; set; }

[JsonProperty("category_id")]
public long CategoryId { get; set; }

[JsonProperty("media_id")]
public long MediaId { get; set; }

[JsonProperty("id")]
public long Id { get; set; }
}
}

然后在你的类里面这样做:

var info = JsonConvert.DeserializeObject<JsonModel>(json);
var featured = info.Featured;
var categories = info.Categories;

关于c# - 使用 LINQ 将 JSON 解析为对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50963363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com