【问题标题】:Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型
【发布时间】:2016-02-05 20:05:36
【问题描述】:
{
    "RESPONSE": {
        "getitem": {
            "alternativeAddress": {
                "SUCCESS": true,
                "result": {
                    "alternativeaddress": [{
                            "id": "1",
                            "user_type_id": "1",
                            "last_updated_by": null,
                            "code": null,
                            "card": null,
                            "user_name": "Admin",
                            "password": "dd213d69e3ccba0e787ca4ba5d726521",
                            "mobile": null,
                            "email": null,
                            "ip_address": "::1",
                            "last_activity": "2015-10-18 12:16:35",
                            "is_active": "1",
                            "last_update": null,
                            "api_key": null,
                            "card_type_id": null
                        }, {
                            "id": "2",
                            "user_type_id": "2",
                            "last_updated_by": "6",
                            "code": null,
                            "card": "",
                            "user_name": "Waiter",
                            "password": "aa604642b4e7dc479ff84cd12b89f979",
                            "mobile": "",
                            "email": "",
                            "ip_address": "192.168.1.101",
                            "last_activity": "2015-10-15 14:12:43",
                            "is_active": "1",
                            "last_update": "2015-05-25 19:39:01",
                            "api_key": "NdX54QbEY7wuE5wGtMyffjc7RJuABn2drP9kvbPh",
                            "card_type_id": null
                        }, {
                            "id": "3",
                            "user_type_id": "3",
                            "last_updated_by": "6",
                            "code": null,
                            "card": "",
                            "user_name": "Special Admin",
                            "password": "dd213d69e3ccba0e787ca4ba5d726521",
                            "mobile": "",
                            "email": "",
                            "ip_address": "192.168.1.73",
                            "last_activity": "2015-07-28 17:35:40",
                            "is_active": "1",
                            "last_update": "2015-05-25 19:48:13",
                            "api_key": "HH3NzB3P22de6XwBTQV3y6vjgTB1CphaVaPkCGYu",
                            "card_type_id": null
                        }, {
                            "id": "4",
                            "user_type_id": "4",
                            "last_updated_by": null,
                            "code": null,
                            "card": null,
                            "user_name": "Special",
                            "password": "dd213d69e3ccba0e787ca4ba5d726521",
                            "mobile": null,
                            "email": null,
                            "ip_address": "192.168.1.101",
                            "last_activity": "2015-10-15 12:16:35",
                            "is_active": "1",
                            "last_update": null,
                            "api_key": null,
                            "card_type_id": null
                        }, {
                            "id": "5",
                            "user_type_id": "5",
                            "last_updated_by": null,
                            "code": null,
                            "card": null,
                            "user_name": "Customer",
                            "password": "dd213d69e3ccba0e787ca4ba5d726521",
                            "mobile": null,
                            "email": null,
                            "ip_address": null,
                            "last_activity": null,
                            "is_active": "1",
                            "last_update": null,
                            "api_key": null,
                            "card_type_id": null
                        }, {
                            "id": "6",
                            "user_type_id": "6",
                            "last_updated_by": "3",
                            "code": null,
                            "card": "",
                            "user_name": "Jamal",
                            "password": "0c5b27e54f1905d8e9d375137a194d6c",
                            "mobile": "",
                            "email": "",
                            "ip_address": "77.98.90.37",
                            "last_activity": "2015-06-24 10:08:45",
                            "is_active": "1",
                            "last_update": "2015-06-21 13:25:40",
                            "api_key": "BK4y4eNyCJftwLLV3dhsYKDGVEXggeYHPS6JXJ7q",
                            "card_type_id": null
                        }, {
                            "id": "7",
                            "user_type_id": "1",
                            "last_updated_by": "1",
                            "code": null,
                            "card": "",
                            "user_name": "Test Admion",
                            "password": "7f339f9f931275d42830983c7d0bd20a",
                            "mobile": "",
                            "email": "testadmin@webkutir.net",
                            "ip_address": null,
                            "last_activity": null,
                            "is_active": "0",
                            "last_update": "2015-10-27 08:52:44",
                            "api_key": "v9gaqW4jmL5wWvYeMwSBbVHrQhraBTfVTnV89Qgm",
                            "card_type_id": null
                        }]
                }
            }
        }
    } }

【问题讨论】:

  • 你能正确地格式化你的问题吗?
  • 连同格式,请阅读stackoverflow.com/help/how-to-ask
  • 我认为你甚至没有尝试任何东西。只是期待准备好从 SO 粘贴代码。

标签: c# json desktop-application


【解决方案1】:

定义如下类结构:

public class Alternativeaddress2
{
    public string id { get; set; }
    public string user_type_id { get; set; }
    public string last_updated_by { get; set; }
    public object code { get; set; }
    public string card { get; set; }
    public string user_name { get; set; }
    public string password { get; set; }
    public string mobile { get; set; }
    public string email { get; set; }
    public string ip_address { get; set; }
    public string last_activity { get; set; }
    public string is_active { get; set; }
    public string last_update { get; set; }
    public string api_key { get; set; }
    public object card_type_id { get; set; }
}

public class Result
{
    public List<Alternativeaddress2> alternativeaddress { get; set; }
}

public class AlternativeAddress
{
    public bool SUCCESS { get; set; }
    public Result result { get; set; }
}

public class Getitem
{
    public AlternativeAddress alternativeAddress { get; set; }
}

public class RESPONSE
{
    public Getitem getitem { get; set; }
}

public class RootObject
{
    public RESPONSE RESPONSE { get; set; }
}

并以这种方式使用它:

var obj = JsonConvert.DeserializeObject<RootObject>(jsonString);

Console.WriteLine(obj.RESPONSE.getitem.alternativeAddress.SUCCESS); // for example

【讨论】:

  • 感谢您的解决方案
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
相关资源
最近更新 更多