Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
247 views
Welcome To Ask or Share your Answers For Others

1 Answer

I'm not very familiar with JsonConvert or Json.NET so I'm not sure what options are available for that. Personally I'd just call the deserializer again immediately afterwards.

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

You could move this into the class if you wanted and call it directly off the deserialized class.

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

    internal UserContext Deserialize()
    {
        // Serialize the object
        this.AuthObject = JsonConvert.DeserializeObject<Auth>(this.AuthValue);

        // Return this object for a simple single-line call.
        return this;
    }
}

// Single object
UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context).Deserialize();

// Enumeration of object (given that this is supported in JsonConvert)
IEnumerable<UserContext> conObjs = JsonConvert.DeserializeObject<IEnumerable<UserContext>(contexts).Select(c => c.Deserialize()).ToList();

Or if you feel self hating you could go as far as doing the deserialization at the time the property is accessed (although I would avoid this at almost all costs due to the numerous issues it can cause).

public class UserContext
{
    private Auth m_auth;

    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
    public Auth AuthObject
    {
        get
        {
            if (this.m_auth == null)
            {
                this.m_auth = JsonConvert.DeserializeObject<Auth>(this.AuthValue);
            }

            return this.m_auth;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...