在反序列化json数据之后,所有对象都将为null

时间:2018-08-06 10:17:30

标签: c# json

我正在尝试将JSON反序列化为自定义对象,但是我所有的属性都设置为null,并且不确定发生了什么。有人看到有什么问题吗?

{
categories: [
    {
        id: 1,
        parent_id: null,
        category_name: "Health",
        url: "uploads/categories/category5.png",
        file_name: "category5.png",
        created_at: "2018-08-06 09:52:45",
        updated_at: "2018-08-06 09:52:45",
        deleted_at: null
    }
],
items: [
    {
        id: 1,
        item_name: "Item",
        hits: 0,
        file_name: "12._dental_leaflet.pdf",
        file_url: "uploads/docs/12._dental_leaflet.pdf",
        thumb: "category4.png",
        thumb_url: "uploads/docs/thumb/category4.png",
        type: "pdf",
        category_id: 1,
        created_at: "2018-08-06 09:57:04",
        updated_at: "2018-08-06 09:57:04",
        deleted_at: null
    }
]
}

我在这里上了三堂课。类别之一,一项用于项目。并创建一个收集类来绑定它们。但是我做错了。我找不到我做错了的地方。请给我一些解决方案。并帮助我找出我在哪里做错了

class Category
    {
        int id;
        int parent_id;
        string category_name;
        string url;
        string file_name;
        string created_at;
        string updated_at;
        string deleted_at;

        public int Id { get => id; set => id = value; }
        public int Parent_id { get => parent_id; set => parent_id = value; }
        public string Category_name { get => category_name; set => category_name = value; }
        public string Url { get => url; set => url = value; }
        public string File_name { get => file_name; set => file_name = value; }
        public string Created_at { get => created_at; set => created_at = value; }
        public string Updated_at { get => updated_at; set => updated_at = value; }
        public string Deleted_at { get => deleted_at; set => deleted_at = value; }
    }


class Item
    {
        int id;
        int category_id;
        string item_name;
        string file_url;
        string thumb;
        string thumb_url;
        string type;
        string created_at;
        string updated_at;
        string deleted_at;

        public int Id { get => id; set => id = value; }
        public int Category_id { get => category_id; set => category_id = value; }
        public string Item_name { get => item_name; set => item_name = value; }
        public string File_url { get => file_url; set => file_url = value; }
        public string Thumb { get => thumb; set => thumb = value; }
        public string Thumb_url { get => thumb_url; set => thumb_url = value; }
        public string Type { get => type; set => type = value; }
        public string Created_at { get => created_at; set => created_at = value; }
        public string Updated_at { get => updated_at; set => updated_at = value; }
        public string Deleted_at { get => deleted_at; set => deleted_at = value; }
    }


class JsonCollection
    {
        private List<Category> categories;
        private List<Item> items;

        public List<Category> Categories { get => categories; set => categories = value; }
        public List<Item> Items { get => items; set => items = value; }

    }

我的反序列化代码是

    WebClient webClient = new WebClient();

    string rawJson = webClient.DownloadString(url);
    JsonCollection jsonCollection = JsonConvert.DeserializeObject<JsonCollection>(rawJson);

这里的问题是jsonCollection显示为空。我是C#的新手。我正在尝试各种方式。这需要我一整天。我没有发现我做错了什么。请给我一些解决方案。

0 个答案:

没有答案