parse nested json string in c#

i have json string as

{"AccountNo":"345234533466","AuthValue":"{\"TopUpMobileNumber\":\"345234533466\",\"VoucherAmount\":\"100\"}"}

to parse this string i have created class as

public class UserContext
{
    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
}

in AuthValue it gives me output as {\"TopUpMobileNumber\":\"345234533466\",\"VoucherAmount\":\"100\"} which is absolutely correct. now i want to modify my class in such way that i want AuthValue in string format as well and in seprate member variable format.

so i modify my class in this way but it gives error

public class UserContext
{
    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
    public Auth ????? { get; set; }
}

 public class Auth
{
    public string TopUpMobileNumber { get; set; }
    public string VoucherAmount { get; set; }
}

My requirement is

  1. AuthValue whole json string i required
  2. in another variable i want member wise values

Parsing Logic

UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context);

Note : No modification in json string is allowed.

Jon Skeet
people
quotationmark

I would suggest using two classes - one for the JSON you're actually receiving, and then one for the object model you want to use:

public class JsonUserContext
{
    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
}

public class UserContext
{
    public string AccountNo { get; set; }
    public Auth AuthValue { get; set; }
}

public class Auth
{
    public string TopUpMobileNumber { get; set; }
    public string VoucherAmount { get; set; }
}

...

var jsonUserContext = JsonConvert.DeserializeObject<JsonUserContext>(json);
var authJson = jsonUserContext.AuthValue;
var userContext = new UserContext {
    AccountNo = jsonUserContext.AccountNo,
    AuthValue = JsonConvert.DeserializeObject<JsonUserContext>(authJson);
};

people

See more on this question at Stackoverflow