【问题标题】:Deserializing list of items反序列化项目列表
【发布时间】:2016-07-12 21:32:29
【问题描述】:

我有以下示例 Json 字符串:

{"Items":[{"Id":"20","CaptureCategoryTypeId":5021,"Name":"24270","Description":"FSH  CARRIBEAN CAPTAIN","IsEnabled":true}],"TotalResults":0}

我需要反序列化,但我不想保留我的类名如下:

public class Item
{
    public string Id { get; set; }
    public int CaptureCategoryTypeId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool IsEnabled { get; set; }
}

public class RootObject
{
    public List<Item> Items { get; set; }
    public int TotalResults { get; set; }
}

我想保留自定义类名,例如 DataDetails。如何在 c# 中实现?

【问题讨论】:

  • 你想保留自定义类名是什么意思?是否要将 RootObject 重命名为 DataDetails?
  • 是的 rootobject 到别的东西和 Item as Data details。
  • @Cchanchal 我添加了一个答案,它工作正常。
  • @Tech1337:非常感谢!

标签: c# .net json serialization deserialization


【解决方案1】:

我不太明白您的问题是什么,但您可以将类名更改为您想要的任何名称。下面是一个工作示例。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string JSONInput = @"{""Items"":[{""Id"":""20"",""CaptureCategoryTypeId"":5021,""Name"":""24270"",""Description"":""FSH CARRIBEAN CAPTAIN"",""IsEnabled"":true}],""TotalResults"":0}";
            BlahObject deserializedProduct = JsonConvert.DeserializeObject<BlahObject>(JSONInput);
            Console.ReadKey();
        }
    }

    public class DataDetails
    {
        public string Id { get; set; }
        public int CaptureCategoryTypeId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public bool IsEnabled { get; set; }
    }

    public class BlahObject
    {
        public List<DataDetails> Items { get; set; }
        public int TotalResults { get; set; }
    }
}

【讨论】:

  • 谢谢我正是想要这个!
猜你喜欢
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
相关资源
最近更新 更多