简体   繁体   English

JsonConvert.DeserializeObject 返回 null asp.net core

[英]JsonConvert.DeserializeObject return null asp.net core

Here is the call to DeserializeObject:这是对 DeserializeObject 的调用:

var classcateg = JsonConvert.DeserializeObject<CategoryDto>(ResponJSON);

but i still get null in classcateg但我仍然在 classcateg 中得到 null

this is my code :这是我的代码:

CategoryDto.cs https://pastebin.com/ktKnvgdK CategoryDto.cs https://pastebin.com/ktKnvgdK

CategoriesRootObject.cs https://pastebin.com/tpxjp5em类别RootObject.cs https://pastebin.com/tpxjp5em

respon of json json 的响应

{
    "categories": [
        {
            "name": "KIDS",
            "localized_names": [
                {
                    "language_id": 1,
                    "localized_name": "KIDS"
                },
                {
                    "language_id": 3,
                    "localized_name": "KIDS"
                },
                {
                    "language_id": 4,
                    "localized_name": "KIDS"
                },
                {
                    "language_id": 2,
                    "localized_name": "KIDS"
                }
            ],
            "description": "<p>Get Ahead The List of Our 'Kids Collection'</p>",
            "category_template_id": 1,
            "meta_keywords": null,
            "meta_description": null,
            "meta_title": null,
            "parent_category_id": 0,
            "page_size": 6,
            "page_size_options": "6, 3, 9",
            "price_ranges": null,
            "show_on_home_page": null,
            "include_in_top_menu": true,
            "has_discounts_applied": null,
            "published": true,
            "deleted": false,
            "display_order": 0,
            "created_on_utc": "2020-09-17T02:17:25.7941446",
            "updated_on_utc": "2020-09-21T03:03:19.1346274",
            "role_ids": [],
            "discount_ids": [],
            "store_ids": [],
            "image": {
                "src": "https://bateeq-nop-dev.azurewebsites.net/images/thumbs/0002114_kids.jpeg",
                "attachment": null
            },
            "se_name": "kids",
            "id": 20
        }
    ]
}

Any one can help me to solve this?任何人都可以帮我解决这个问题吗? thanks before之前谢谢

Here's a sample of text that works:这是一个有效的文本示例:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public class Program
{
    public static void Main()
    {
        var jsonString = "{ \"categories\": [ { \"name\": \"KIDS\", \"localized_names\": [ { \"language_id\": 1, \"localized_name\": \"KIDS\" }, { \"language_id\": 3, \"localized_name\": \"KIDS\" }, { \"language_id\": 4, \"localized_name\": \"KIDS\" }, { \"language_id\": 2, \"localized_name\": \"KIDS\" } ], \"description\": \"<p>Get Ahead The List of Our 'Kids Collection'</p>\", \"category_template_id\": 1, \"meta_keywords\": null, \"meta_description\": null, \"meta_title\": null, \"parent_category_id\": 0, \"page_size\": 6, \"page_size_options\": \"6, 3, 9\", \"price_ranges\": null, \"show_on_home_page\": null, \"include_in_top_menu\": true, \"has_discounts_applied\": null, \"published\": true, \"deleted\": false, \"display_order\": 0, \"created_on_utc\": \"2020-09-17T02:17:25.7941446\", \"updated_on_utc\": \"2020-09-21T03:03:19.1346274\", \"role_ids\": [], \"discount_ids\": [], \"store_ids\": [], \"image\": { \"src\": \"https://bateeq-nop-dev.azurewebsites.net/images/thumbs/0002114_kids.jpeg\", \"attachment\": null }, \"se_name\": \"kids\", \"id\": 20 } ] }";
        var x = JsonConvert.DeserializeObject<Category>(jsonString);
        
        
        Console.WriteLine(x.Categories.First().Description);
    }
}

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

public partial class CategoryElement
{
    [JsonProperty("name")]
    public string Name
    {
        get;
        set;
    }

    [JsonProperty("localized_names")]
    public List<LocalizedName> LocalizedNames
    {
        get;
        set;
    }

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

    [JsonProperty("category_template_id")]
    public long CategoryTemplateId
    {
        get;
        set;
    }

    [JsonProperty("meta_keywords")]
    public object MetaKeywords
    {
        get;
        set;
    }

    [JsonProperty("meta_description")]
    public object MetaDescription
    {
        get;
        set;
    }

    [JsonProperty("meta_title")]
    public object MetaTitle
    {
        get;
        set;
    }

    [JsonProperty("parent_category_id")]
    public long ParentCategoryId
    {
        get;
        set;
    }

    [JsonProperty("page_size")]
    public long PageSize
    {
        get;
        set;
    }

    [JsonProperty("page_size_options")]
    public string PageSizeOptions
    {
        get;
        set;
    }

    [JsonProperty("price_ranges")]
    public object PriceRanges
    {
        get;
        set;
    }

    [JsonProperty("show_on_home_page")]
    public object ShowOnHomePage
    {
        get;
        set;
    }

    [JsonProperty("include_in_top_menu")]
    public bool IncludeInTopMenu
    {
        get;
        set;
    }

    [JsonProperty("has_discounts_applied")]
    public object HasDiscountsApplied
    {
        get;
        set;
    }

    [JsonProperty("published")]
    public bool Published
    {
        get;
        set;
    }

    [JsonProperty("deleted")]
    public bool Deleted
    {
        get;
        set;
    }

    [JsonProperty("display_order")]
    public long DisplayOrder
    {
        get;
        set;
    }

    [JsonProperty("created_on_utc")]
    public string CreatedOnUtc
    {
        get;
        set;
    }

    [JsonProperty("updated_on_utc")]
    public string UpdatedOnUtc
    {
        get;
        set;
    }

    [JsonProperty("role_ids")]
    public List<object> RoleIds
    {
        get;
        set;
    }

    [JsonProperty("discount_ids")]
    public List<object> DiscountIds
    {
        get;
        set;
    }

    [JsonProperty("store_ids")]
    public List<object> StoreIds
    {
        get;
        set;
    }

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

    [JsonProperty("se_name")]
    public string SeName
    {
        get;
        set;
    }

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

public partial class Image
{
    [JsonProperty("src")]
    public Uri Src
    {
        get;
        set;
    }

    [JsonProperty("attachment")]
    public object Attachment
    {
        get;
        set;
    }
}

public partial class LocalizedName
{
    [JsonProperty("language_id")]
    public long LanguageId
    {
        get;
        set;
    }

    [JsonProperty("localized_name")]
    public string LocalizedNameLocalizedName
    {
        get;
        set;
    }
}

Output:输出:

<p>Get Ahead The List of Our 'Kids Collection'</p>

https://dotnetfiddle.net/VUqodH https://dotnetfiddle.net/VUqodH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM