简体   繁体   English

如何在asp.net中反序列化JSON数据?

[英]How to deserialize JSON data in asp.net?

I have following JSON data, class and code but it doesn't working. 我有以下JSON数据,类和代码,但是它不起作用。 I am getting List of 3 object but all are NULL. 我正在获取3个对象的列表,但都为NULL。 Can anybody please suggest me what I am missing here? 有人可以建议我这里我所缺少的吗?

JSON data: JSON数据:

[
    {
        "donotpostalmail": {
            "Do Not Allow": {
                "Do Not Allow": 1
            },
            "Allow": {
                "Allow": 0
            }
        }
    },
    {
        "familystatuscode": {
            "Single": {
                "Single": 1
            },
            "Married": {
                "Married": 2
            },
            "Divorced": {
                "Divorced": 3
            },
            "Widowed": {
                "Widowed": 4
            }
        }
    },
    {
        "preferredcontactmethodcode": {
            "Any": {
                "Any": 1
            },
            "Email": {
                "Email": 2
            },
            "Phone": {
                "Phone": 3
            },
            "Fax": {
                "Fax": 4
            },
            "Mail": {
                "Mail": 5
            }
        }
    }
]

Class: 类:

public class ResponseDataOfOptions
{
    public OptionsList mainList { get; set; }
}
public class OptionsList
{
    public Dictionary<string, Options> optionList { get; set; }
}
public class Options
{
    public Dictionary<string, int> options { get; set; }
}

.cs file Code: .cs文件代码:

List<ResponseDataOfOptions> optionList = JsonConvert.DeserializeObject<List<ResponseDataOfOptions>>(objResponse.ResponseDataOfOptions);

You cannot use objects in this case because every object has different property names. 在这种情况下,您不能使用对象,因为每个对象都有不同的属性名称。 The only way to deserialize this JSON is as follows. 反序列化此JSON的唯一方法如下。

 JsonConvert.DeserializeObject<List<
      Dictionary<string, 
          Dictionary<string, 
              Dictionary<string, int>
          >
      >
 >>(objResponse.ResponseDataOfOptions);

It is a list of dictionaries, in fact there are three levels of dictionaries. 它是字典的列表,实际上有三个级别的字典。 It's not a very friendly structure to work with data but that would do the deserialize. 处理数据不是一个非常友好的结构,但是会反序列化。

You might also be interested in using some property detection using JObject and JToken and then inflate appropriate data transfer objects. 您可能还对使用JObjectJToken进行某些属性检测,然后为适当的数据传输对象进行膨胀感兴趣。

JObject list = JObject.Parse(objResponse.ResponseDataOfOptions);
foreach (var item in list) {
    JToken token = jobj["donotpostalmail"];
    if (token != null) {
        // inflate the corresponding data type.
    }
}

You cannot use an array that contains 3 different objects types. 您不能使用包含3种不同对象类型的数组。 If you want to do this, I recommend to use a container object instead of an array: 如果要执行此操作,我建议使用容器对象而不是数组:

{
    "responseDataOfOptions": {
        "donotpostalmail": {
            "Do Not Allow": {
                "Do Not Allow": 1
            },
            "Allow": {
                "Allow": 0
            }
        }
    },
    "optionsList": {
        "familystatuscode": {
            "Single": {
                "Single": 1
            },
            "Married": {
                "Married": 2
            },
            "Divorced": {
                "Divorced": 3
            },
            "Widowed": {
                "Widowed": 4
            }
        }
    },
    "options": {
        "preferredcontactmethodcode": {
            "Any": {
                "Any": 1
            },
            "Email": {
                "Email": 2
            },
            "Phone": {
                "Phone": 3
            },
            "Fax": {
                "Fax": 4
            },
            "Mail": {
                "Mail": 5
            }
        }
    }
}

The corresponding C# class must look like this: 相应的C#类必须如下所示:

public class JsonResponse {
    public ResponseDataOfOptions responseDataOfOptions{ get; set; }
    public OptionsList optionsList{ get; set; }
    public Options options { get; set; }
}

please check your json string objResponse.ResponseDataOfOptions format whether is correct, maybe there are some &quot; 请检查您的json字符串objResponse.ResponseDataOfOptions格式是否正确,也许有些&quot; in your string, if yes, you need to replace these &quot; 在字符串中,如果是,则需要替换这些&quot; as below: 如下:

List<ResponseDataOfOptions> optionList = JsonConvert.DeserializeObject<List<ResponseDataOfOptions>>(objResponse.ResponseDataOfOptions.Replace("&quot;", "\""));

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

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