locked
Json deserialization RRS feed

  • Question

  • Hello everyone,

    I am some trouble to login page using api. So please help me. I am new in windows phone development.

    This is my code file(mainpage.cs):

    private void btnlogin_Click_1(object sender, RoutedEventArgs e)
            {
                Logindetail detail = new Logindetail();
                detail.email = txtname.Text;
                detail.password = txtpassword.Password;
                string jsondata = JsonConvert.SerializeObject(detail);
                webclient.Headers["Content-type"] = "application/json";
                webclient.Encoding = Encoding.UTF8;
                Uri uri = new Uri("abc", UriKind.RelativeOrAbsolute);
                webclient.UploadStringCompleted += webclient_UploadStringCompleted;
                webclient.UploadStringAsync(uri, "POST", jsondata);
                Debug.WriteLine(jsondata);
            }

    and my user.cs is:

    public class User
        {
            public string token { get; set; }
            public string first_name { get; set; }
            public string last_name { get; set; }
            public string email_id { get; set; }
            public string username { get; set; }
            public string birth_date { get; set; }
            public string phone_no { get; set; }
            public string image { get; set; }
            public string gender { get; set; }
            public string address { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public string zip_code { get; set; }
            public string userid { get; set; }
        }
    
        public class RootObject
        {
            public User User { get; set; }
        }

    So the e.result data is look like:

    {
      "successfull": "true",
      "User": {
        "token": "39kn7sph",
        "first_name": "admin",
        "last_name": "admin",
        "email_id": "admin@priceball.com",
        "username": "admin",
        "birth_date": "1370131200",
        "phone_no": "9724556552",
        "image": "img/uploads/images/users/426850952409ed453d7a48ddfa449561.jpg",
        "gender": "M",
        "address": "ahmedabad",
        "city": "NB",
        "state": "CA",
        "zip_code": "3800123",
        "userid": "1"
      }
    }

    So what i am doing to deserialize that e.result? so what i am write in this to deserialize e.result & get the data..

     private void webclient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
            {
                }

    Saturday, March 16, 2013 1:15 PM

Answers

  • Try to this code:

     RootObject rd = JsonConvert.DeserializeObject<RootObject>(e.Result);

    • Marked as answer by Khant Nipun Monday, March 18, 2013 5:43 AM
    Monday, March 18, 2013 5:43 AM

All replies

  • For deserialization you can use RestSharpDataContractJsonSerializer or Json.NET. Pick which you like, they all have documentation on how to use them.

    Toni Petrina
    My blog: Toni codes .NET
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

    Sunday, March 17, 2013 11:09 AM
  • Try to this code:

     RootObject rd = JsonConvert.DeserializeObject<RootObject>(e.Result);

    • Marked as answer by Khant Nipun Monday, March 18, 2013 5:43 AM
    Monday, March 18, 2013 5:43 AM