简体   繁体   中英

Deserialize JSON into C# Object

I am trying to deserialize a json object into c# object but couldnt do it. Here is my json object that I sending

{
    "User_":
    {
        "Email":"test@test.com",
        "Password":"pass1"
    }
}

here is my c# classes that I use

public class User_
{
    public string Email { get; set; }
    public string Password { get; set; }
}

public class User_ { public string Email { get; set; } public string Password { get; set; } }

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

here is the code that I use to deserialize

RootObject request = JsonConvert.DeserializeObject<RootObject>(json.ToString());

This code creates a User_ object but Email and Password fields are null. What am I doing wrong here?

While I was debugging, I saw what is coming

{{ "{\"Email_\":\"test@test.com\",\"Password_\":\"pass1\"}": ""  }}

It looks weird

When deserializing object of a class directly, The class name is not required in JSON. If you are on the other hand deserializing the JSON given above, then deserialize using RootObject

var user = JsonConvert.DeserializeObject<RootObject>(json.ToString()).User_;

with

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

OR

Deserialize the following JSON instead:

    {
        "Email":"test@test.com",
        "Password":"pass1"
    }

If I do this, I get the correct values of Email and Password :

public class User_
  {
    public string Email { get; set; }
    public string Password { get; set; }
  }

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

  class Program
  {
    static void Main(string[] args)
    {
      string jsonString = "{\"User_\":{\"Email\":\"test@test.com\",\"Password\":\"pass1\"}}";
      RootObject request = JsonConvert.DeserializeObject<RootObject>(jsonString.ToString());
      Console.WriteLine("Email: {0}", request.User_.Email);
      Console.WriteLine("Password: {0}", request.User_.Password);
      Console.ReadLine();
    }
  }

You can see it working HERE

Why not use [JsonProperty("KeyName")] when your key names are not pure alphabets.

[JsonProperty("KeyName")] provide you an advantage of deserializing such json that contains some keys like

  • User_
  • User$
  • @User
  • *User&

And many more

Just add this attribute to your class property like

[JsonProperty("User_")]
public User User { get; set; }

And this makes your property name keep simple and more readable

So finally for your json

{
    "User_":
    {
        "Email_":"test@test.com",
        "Password_":"pass1"
    }
}

All classes after adding JsonProperty look like

public class RootObject
{
    [JsonProperty("User_")]
    public User User { get; set; }
}

public class User
{
    [JsonProperty("Email_")]
    public string Email { get; set; }

    [JsonProperty("Password_")]
    public string Password { get; set; }
}

Usage:

RootObject request = JsonConvert.DeserializeObject<RootObject>(json.ToString());

Console.WriteLine(request.User.Email);
Console.WriteLine(request.User.Password);

Output:

在此处输入图片说明

I believe you have 2 issues in your c# class

  1. In your RootObject class you have to use

    public User_ User{get;set;}
  2. When you seralize Json you have to use.

     User_ request = JsonConvert.DeserializeObject<RootObject>(json.ToString());

This will solve your issue.

Can you try this code.

public class User_
{
    public string Email { get; set; }
    public string Password { get; set; }
}

public class RootObject
{
    public User_ User { get; set; }
}

Deserialize operation:

RootObject request = JsonConvert.DeserializeObject<RootObject>(json.ToString());

The main issue is that your RootObject is serialise as the property name of a new object with string empty as value. The result of to many serialisation server side.

And you can handle those easly, the Following class will work only for this exact Json response once the password/email change you will need a new one.

public partial class DontDoThis
{
    [JsonProperty("{\"Email_\":\"test@test.com\",\"Password_\":\"pass1\"}")]
    public string EmailTestTestComPasswordPass1 { get; set; }
}

In order to fix this you are left with:
- Change the code server side to provide correct result.
- String manipulation to remove and un escape the result.
- Deserialise once in and deseriliase again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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