代码之家  ›  专栏  ›  技术社区  ›  Delta76

将JSON字符串转换为“复杂”类?

  •  3
  • Delta76  · 技术社区  · 14 年前

    我有一个JSON字符串:

     {
    "success":true,"user_id":"309","id":"309","sessId":false,"email":null,"name":"Mai Van Quan","username":"quanmv","role":"Reseller Admin","messages":"","org_name":null,"microPayNumber":"4949","microPayWord":"neocam","mobile":null,"permissions":{"ADD_CAMERA":true,
        "REMOVE_CAMERA":true,
        "EDIT_CAM_GENERAL":true,
        "ACCESS_CAM_TECHNICAL":true,
        "EDIT_CAM_PKG":true,
        "PREVIEW_CAM":true
        },"type":"sp","time":1279702793,"reseller":1,"status":"ok"}
    

    我想用JSON.NET把它转换成一个C对象。NET可以将其转换为“泛型”对象,但我想将其转换为更具体的对象。我创建了这个类:

    internal class User
        {
    
            public User(User u)
            {
                status = u.status;
                id = u.id;
                sessId = u.sessId;
                email = u.email;
                username = u.username;
                role = u.role;
                messages = u.messages;
                org_name = u.org_name;
                microPayNumber = u.microPayNumber;
                microPayWorld = u.microPayWorld;
                mobile = u.mobile;
                permissions = u.permissions;
                type = u.type;
                time = u.time;
                reseller = u.reseller;
                status = u.status;
            }
    
            public bool successs { private set; get; }
    
    
            public string user_id { private set; get; }
            public string id { private set; get; }
    
    
    
            public string name { private set; get; }
            public bool sessId { private set; get; }
            public string email { private set; get; }
    
            public string username { private set; get; }
    
            public string role { private set; get; }
            public string messages { private set; get; }
            public string org_name{ private set; get; }
            public string microPayNumber { private set; get; }
            public string microPayWorld { private set; get; }
            public string mobile { private set; get; }
            public Dictionary<string,bool> permissions { private set; get; }
            public string type { private set; get; }
    
            public int time { private set; get; }
    
            public int reseller { private set; get; }
            public string status { private set; get; }
    
    }
    

    编辑:例如:

    var ob = JsonConvert.DeserializeObject<User>(str);
    

    异常:异常已由调用的目标引发。

    如何有效地将此字符串转换为对象,因为需要转换的字符串类型不止一种。

    谢谢你

    2 回复  |  直到 14 年前
        1
  •  8
  •   Samuel Meacham    14 年前

    一些小事情,我有你的代码运行良好。

    第一件事:你得到了一个NullReferenceException。这个 permissions 你的财产 User 类从未实例化。Json.NET不会为您创建它。我将此默认构造函数添加到 用户 班级:

    public User()
    {
        permissions = new Dictionary<string, bool>();
    }
    

    private internal 属性的设置器 班级。一旦我把它们改成 public

    更新

    还有一个发现。如果你给你的 带a的班级 [JsonObject] 属性,并使用 [JsonProperty] 属性,则您可以将setters保留为 私有的 内部的