简体   繁体   English

反序列化字符串数组以列出 C#

[英]Deserialize string array to list C#

I've raw JSON data that looks like this:我有如下所示的原始 JSON 数据:

{
    "Pages": 0,
    "Page": null,
    "Rows": null,
    "OrderBy": null,
    "Desc": false,
    "TotalRows": 36,
    "HasError": false,
    "Error": null,
    "Result": {
        "fileTypes": [
            {
                "id": 22,
                "description": "Cleaning Procedures",
                "Codes": ""
            },
            {
                "id": 32,
                "description": "Data Drop",
                "Codes": "[\"DB\",\"IE\"]"
            },
            {
                "id": 4,
                "description": "Data Sheet",
                "Codes": ""
            },
            {
                "id": 30,
                "description": "Description of Operation",
                "Codes": ""
            },
            {
                "id": 11,
                "description": "Diagram",
                "Codes": ""
            },
            {
                "id": 2,
                "description": "Drawing",
                "Codes": "[\"DR\"]"
            }
        ]
    }
}

And a C# Model that looks like this:和一个看起来像这样的 C# Model

public class Filetype
{
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("Codes")]
    //public string Codes { get; set; }

    public List<string> Codes { get; set; }
}

However, Newtonsoft is not deserializing the codes into a list of codes but rather returning但是,Newtonsoft 不会将代码反序列化为代码列表,而是返回

[\\"DB\\",\\"IE\\"] in the first element of the list , or , "" , ( null ). [\\"DB\\",\\"IE\\"]在列表的第一个元素中,或 , "" , ( null )。

Any help with this would be appreciated.对此的任何帮助将不胜感激。

code has de following value "["DB","IE"]" but list in json does not need quotes代码具有以下值 "["DB","IE"]" 但json 中的列表不需要引号

the value of codes in json needs to be like this json中代码的值需要是这样的

 "Codes": ["DB","IE"]

Client Side :客户端 :

If one of the property is a string serialisation of a Json.如果属性之一是 Json 的字符串序列化。
You can add a Property with [JsonIgnore] that represent the result of the deserialisation :您可以使用[JsonIgnore]添加一个表示反序列化结果的属性:

public class Foo{
    public string BarsInString {get;set;}       
    [JsonIgnore]
    public Bar[] Bars 
    {
        get 
         {
            return JsonConvert.DeserializeObject<Bar[]>(UserInput);
         }
       set 
        {
           BarsInString = JsonConvert.SerializeObject(value);
        }     
    }
}

Live Demo: https://dotnetfiddle.net/l5JWVb现场演示: https : //dotnetfiddle.net/l5JWVb

I chooseed something bigger than an List<string> to illustrate.我选择了比List<string>更大的东西来说明。 In your case it will boils down to:在您的情况下,它将归结为:

public class Filetype
{
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("Codes")]
    public string CodesInJson { get; set; }

    [JsonIgnore]
    public List<string> Codes {
        get 
         {
            return JsonConvert.DeserializeObject<List<string>>(CodesInJson);
         }
       set 
        {
           CodesInJson = JsonConvert.SerializeObject(value);
        }     
    }
}

Server Side:服务器端:

If one of the property is a Json serialisation of something else you may want to deserialise it before sending it.如果属性之一是其他内容的 Json 序列化,您可能希望在发送之前反序列化它。 Using the same kind of code:使用相同类型的代码:

JsonConvert.DeserializeObject<List<string>>(myJsonFromDb);

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

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