3

I have this JSON string:

 {
"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"}

I want to convert it to a C# object, using JSON.NET. JSON.NET can convert it to a "generics" object, but I want to convert it to a more specific object. I created this class:

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; }

}

but JSON.NET seems to be failed to convert the given string to an User object. I tried some methods, but they failed all.

EDIT: for example:

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

exception: Exception has been thrown by the target of an invocation.

How can I convert this string to an object, effectively, because there is more than one type of string need to be converted.

Thank you

4
  • do you get any error-message? Commented Jul 21, 2010 at 9:28
  • 3
    "they failed all" isn't very descriptive. Explain what you tried, and what happened. I wouldn't be entirely surprised if it was failing due to the private property setters. Commented Jul 21, 2010 at 9:28
  • @jon: +1 for seeing the private set... :) Commented Jul 21, 2010 at 9:29
  • @Jon Skeet: I switched all access modifier to "public", the exception still remain :) Commented Jul 21, 2010 at 9:35

2 Answers 2

8

Couple of small things, and I have your code running great.

First thing: You were getting a NullReferenceException. The permissions property of your User class was never instantiated. Json.NET does not create it for you. I added this default constructor to your User class:

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

Second thing: It doesn't work with private or internal setters on the properties of the User class. Once I changed them to public, it worked fine.

UPDATE

One more discovery. If you tag your User class with a [JsonObject] attribute, and each of your properties with a [JsonProperty] attribute, then you can leave your setters as either private or internal, and everything works great.

Sign up to request clarification or add additional context in comments.

Comments

2

Maybe give the WCF JSON serializer a try: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx

Edit: Also have a look at this C# automatic property deserialization of JSON

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.