Josconcept
Josconcept

Reputation: 11

How do i get values from Json object in Xamarin Form

Please i need help, i need to get values for UserID and UserID from the json Object below

{"data":{"UserID":"MS100000041","RoleID":5}}

This is my code:

public class UserDetails
{
    public string UserID { get; set; }
    public int RoleID { get; set; }
}

public  async Task Login(string url)
{
    try
    {
        var uri = new Uri(url);
        HttpClient myClient = new HttpClient();

        var response = await myClient.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            var Item = JsonConvert.DeserializeObject<UserDetails>(content);
            string userid = Item.UserID;

            int roleid= Item.RoleID;
        }
        else
        {
            Application.Current.Properties["response"] = response;
        }

    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
    }
}

But this value userid and roleid are showing null. Please how do i get these value

Upvotes: 1

Views: 3261

Answers (4)

Pucho Eric
Pucho Eric

Reputation: 505

 using Newtonsoft.Json;


            public  async Task Login(string url)
            {
            List<UserDetails> ud = new List<UserDetails> ();
                try
                {
                    var uri = new Uri(url);
                    HttpClient myClient = new HttpClient();

                    var response = await myClient.GetAsync(uri);
                    if (response.IsSuccessStatusCode)
                    {
                        var content = await response.Content.ReadAsStringAsync();
                        JObject results = JObject.Parse (content);

                        var  results2 = results ["data"];

                                foreach (var i in results2) {
                                 ud.Add (new UserDetails () {
                                  UserID = i["UserID"].ToString();
                                  RoleID = int.Parse(i["RoleID"].ToString());
                                 });
                                }
                    }
                    else
                    {
                        Application.Current.Properties["response"] = response;
                    }

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }

Upvotes: 0

Cetin Basoz
Cetin Basoz

Reputation: 23797

To expand on hellowstone's answer:

void Main()
{
    var content = @"{""data"":{""UserID"":""MS100000041"",""RoleID"":5}}";
    var Item = JsonConvert.DeserializeObject<UserDetails>(content);
    Console.WriteLine(Item.data.UserID);
    Console.WriteLine(Item.data.RoleID);
}

public class UserDetails
{
    public class Data
    {
        public string UserID { get; set; }
        public int RoleID { get; set; }
    }
    public Data data { get; set; }
}

Output is:

MS100000041
5

Upvotes: 3

Dan
Dan

Reputation: 2858

as hellowstone pointed out

 [TestClass]
 public class Test2
 {
    [TestMethod]
    public void Test()
    {
        var item = JsonConvert
            .DeserializeObject<Payload>("{\"data\":{\"UserID\":\"MS100000041\",\"RoleID\":5}}")
            .data;
        Assert.AreEqual(item.RoleID, 5);
        Assert.AreEqual(item.UserID, "MS100000041");
    }
    class Payload
    {
        public UserDetails data { get; set; }
    }
    class UserDetails
    {
        public int RoleID { get; set; }
        public string UserID { get; set; }
    }
 }

Upvotes: 2

Halis S.
Halis S.

Reputation: 446

According with the JSON provided, you must access to UserID and RoleID like Item.data.UserID and Item.data.RoleID

Upvotes: 2

Related Questions