How do I extract a single element from a JSON http response and pass it to C# as an integer? -
i'm writing wcf service , trying extract value sms following json string forms http response. rest of response superfluous requirements.
{"balance":{"sms":100,"mms":2},"status":"success"}
the code i've got @ moment ever returns '0' integer balance.
public class balanceobject { public int sms { get; set;} public int mms {get; set;} public string status {get; set;} } public int balancerequest() { using (var wb = new webclient()) { byte[] response = wb.uploadvalues("http://api.txtlocal.com/balance/?apikey=", new namevaluecollection() { {"apikey" , api}, }); string result = system.text.encoding.utf8.getstring(response); javascriptserializer js = new javascriptserializer(); balanceobject request = (balanceobject)js.deserialize(result, typeof(balanceobject)); int balance = request.sms; return balance; } }
any advice gratefully received.
your balanceobject
doesn't match json. object should more this:
public class phoneobject { public string status {get; set;} public balanceobject balance {get; set;} } public class balanceobject { public int sms { get; set;} public int mms {get; set;} }
you retrieve value so:
phoneobject request = (phoneobject)js.deserialize(result, typeof(phoneobject)); int balance = request.balance.sms; return balance;
Comments
Post a Comment