Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...