Thursday 15 July 2010

parse nested json string in c# -



parse nested json string in c# -

i have json string

{"accountno":"345234533466","authvalue":"{\"topupmobilenumber\":\"345234533466\",\"voucheramount\":\"100\"}"}

to parse string have created class as

public class usercontext { public string accountno { get; set; } public string authvalue { get; set; } }

in authvalue gives me output {\"topupmobilenumber\":\"345234533466\",\"voucheramount\":\"100\"} absolutely correct. want modify class in such way want authvalue in string format , in seprate fellow member variable format.

so modify class in way 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

authvalue whole json string required in variable want fellow member wise values

parsing logic

usercontext conobj1 = new usercontext(); conobj1 = jsonconvert.deserializeobject<usercontext>(context);

note : no modification in json string allowed.

i suggest using 2 classes - 1 json you're receiving, , 1 object model want 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); };

c# json

No comments:

Post a Comment