windows phone 8.1 - WP read user_id from JSON and show it -
i want show user_id following json:
[{"username":"kac26","email":"xklemenx@gmail.com","city":"slovenj gradec","id":"15"}]
so save json class. json webpage.
my class:
public class class2 { public string username{ get; set; } public string email { get; set; } public string city { get; set; } public int id { get; set; } } saving json class:
private async void login_tapped(object sender, tappedroutedeventargs e) { string str = user.text; //get user name textbox httpclient http = new httpclient(); var response= await http.getstringasync("http://mywebpage.com/events/apis/user.php/user=" + str); } then want show user id in textbox_id when press button.
private void button_tapped(object sender, tappedroutedeventargs e) { textbox_id.text = class2.id; } but error shows when try reach class2.id know how that? , save json class correct?
thanks!!!
in "saving json class", don't see doing that. here's how deserialize string returned service:
private async void login_tapped(object sender, tappedroutedeventargs e) { string str = user.text; //get user name textbox httpclient http = new httpclient(); var response = await http.getstringasync("http://mywebpage.com/events/apis/user.php/user=" + str); class2 myclass2 = null; try { var jsonserializer = new datacontractjsonserializer(typeof(class2)); using (var stream = new memorystream(system.text.encoding.utf8.getbytes(response))) { myclass2 = jsonserializer.readobject(stream) class2; } } catch { // serialization error occurred } // myclass2 } also, fyi if don't want property names on response class match json properties (for example, not lower case), can use datacontractattribute , datamemberattribute on class so:
[datacontract] public class class2 { [datamember(name = "username")] public string username { get; set; } [datamember(name = "email")] public string email { get; set; } [datamember(name = "city")] public string somecompletelydifferentnameforcity { get; set; } [datamember(name = "id")] public int id { get; set; } }
Comments
Post a Comment