如何使用 Json.Net 将 JSON 字符串解析为 .Net 对象?

发布于 2024-11-05 12:30:51 字数 676 浏览 0 评论 0原文

我要解析的字符串:

[
    {
    id: "new01"
    name: "abc news"
    icon: ""
    channels: [
    {
    id: 1001
    name: "News"
    url: "http://example.com/index.rss"
    sortKey: "A"
    sourceId: "1"
    },
    {
    id: 1002
    name: "abc"
    url: "http://example.com/android.rss"
    sortKey: "A"
    sourceId: "2"
    } ]
    },
{
    id: "new02"
    name: "abc news2"
    icon: ""
    channels: [
    {
    id: 1001
    name: "News"
    url: "http://example.com/index.rss"
    sortKey: "A"
    sourceId: "1"
    },
    {
    id: 1002
    name: "abc"
    url: "http://example.com/android.rss"
    sortKey: "A"
    sourceId: "2"
    } ]
    }
]

The string I want to parse:

[
    {
    id: "new01"
    name: "abc news"
    icon: ""
    channels: [
    {
    id: 1001
    name: "News"
    url: "http://example.com/index.rss"
    sortKey: "A"
    sourceId: "1"
    },
    {
    id: 1002
    name: "abc"
    url: "http://example.com/android.rss"
    sortKey: "A"
    sourceId: "2"
    } ]
    },
{
    id: "new02"
    name: "abc news2"
    icon: ""
    channels: [
    {
    id: 1001
    name: "News"
    url: "http://example.com/index.rss"
    sortKey: "A"
    sourceId: "1"
    },
    {
    id: 1002
    name: "abc"
    url: "http://example.com/android.rss"
    sortKey: "A"
    sourceId: "2"
    } ]
    }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世俗缘 2024-11-12 12:30:51

您的 JSON 实际上不是 JSON - 您需要在字段后添加逗号:

[
    {
    id: "new01",
    name: "abc news",
    icon: "",
    channels: [
    {
    id: 1001,
       ....

假设您已完成此操作并且正在使用 JSON.NET,那么您需要类来表示每个元素 - 主数组中的主要元素以及子“Channel”元素。

类似于:

    public class Channel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string SortKey { get; set; }
        public string SourceId { get; set; }            
    }

    public class MainItem
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }
        public List<Channel> Channels { get; set; }
    }

因为 C# 成员命名约定和 JSON 名称之间不匹配,所以您需要使用映射来装饰每个成员,以告诉 JSON 解析器 json 字段的名称:

    public class Channel
    {
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("sortkey")]
        public string SortKey { get; set; }
        [JsonProperty("sourceid")]
        public string SourceId { get; set; }            
    }

    public class MainItem
    {
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("icon")]
        public string Icon { get; set; }
        [JsonProperty("channels")]
        public List<Channel> Channels { get; set; }
    }

完成此操作后,您可以像这样解析包含 JSON 的字符串:

var result = JsonConvert.DeserializeObject<List<MainItem>>(inputString);

Your JSON isn't actually JSON - you need commas after the fields:

[
    {
    id: "new01",
    name: "abc news",
    icon: "",
    channels: [
    {
    id: 1001,
       ....

Assuming you've done that and that you are using JSON.NET, then you'll need classes to represent each of the elements - the main elements in the main array, and the child "Channel" elements.

Something like:

    public class Channel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string SortKey { get; set; }
        public string SourceId { get; set; }            
    }

    public class MainItem
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }
        public List<Channel> Channels { get; set; }
    }

Because there is a mismatch between the C# member naming conventions and the JSON names, you'll need to decorate each member with a mapping to tell the JSON parser what the json fields are called:

    public class Channel
    {
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("sortkey")]
        public string SortKey { get; set; }
        [JsonProperty("sourceid")]
        public string SourceId { get; set; }            
    }

    public class MainItem
    {
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("icon")]
        public string Icon { get; set; }
        [JsonProperty("channels")]
        public List<Channel> Channels { get; set; }
    }

Once you've done this, you can parse a string containing your JSON like this:

var result = JsonConvert.DeserializeObject<List<MainItem>>(inputString);
谈下烟灰 2024-11-12 12:30:51

是的,它是 JsonConvert.DeserializeObject(json string)

尝试使用 JsonConvert.SerializeObject(object) 创建 JSON。

yup, it's JsonConvert.DeserializeObject(json string)

try using JsonConvert.SerializeObject(object) to create the JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文