Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Getting specific field

Xammy10Xammy10 Member ✭✭✭

public void GetData()
{
string urli = inputUrl.Text.ToString();
string baseAddress = urli;
var response = client.GetAsync(baseAddress + "test/Test").Result;

        data.Text = response.Content.ReadAsStringAsync().Result;
    }

i currently using this as my get method to get all of the data from my web api.

But what i want is to get a specific data in that web api? Like "studentname" only and i will display it on a textview.

Thanks in advance :)

Best Answers

  • Eric_LiraEric_Lira USMember ✭✭✭
    Accepted Answer

    Hi @Enohara
    Create a model representing the studentmodel and deserialize the result from web API

    public class studentmodel
    {
        public int studentID {get; set;}
        public string studentcourse {get; set;}
        public string studentname {get; set;}
    }
    
    public void GetData()
    {
       // ...
       var json = response.Content.ReadAsStringAsync().Result;
       List<studentmodel> studentList = JsonConvert.DeserializeObject<List<studentmodel>>(json);
       // now you can use studentList objet to get any of the values that you need.
       data.Text = studentList.FirstOrDefault().studentname;
    }
    
  • CharwakaCharwaka INMember ✭✭✭✭✭
    edited February 2018 Accepted Answer

    @Enohara said:
    @Eric_Lira i dont get it sir, I'm not using a List, so for what is List? I just want to get the name and post it on the single text view, without the "".

    But still thanks for answering :)

    its not about list , first you need traverse through all elements and get data ,so you need list first to store all data, and fetch required value from that list,so use that code it should work .

  • Eric_LiraEric_Lira USMember ✭✭✭
    Accepted Answer

    Hi @Enohara
    I just showed you as a List because you attached an image showing a xml with a list, I assumed that is what your API returns. If your API can return just one item is basically the same idea, you can just deserialize from json straight to your object, but you need a class representing this object anyway.

    Cheers!

Answers

  • Eric_LiraEric_Lira USMember ✭✭✭
    Accepted Answer

    Hi @Enohara
    Create a model representing the studentmodel and deserialize the result from web API

    public class studentmodel
    {
        public int studentID {get; set;}
        public string studentcourse {get; set;}
        public string studentname {get; set;}
    }
    
    public void GetData()
    {
       // ...
       var json = response.Content.ReadAsStringAsync().Result;
       List<studentmodel> studentList = JsonConvert.DeserializeObject<List<studentmodel>>(json);
       // now you can use studentList objet to get any of the values that you need.
       data.Text = studentList.FirstOrDefault().studentname;
    }
    
  • Xammy10Xammy10 Member ✭✭✭

    @Eric_Lira i dont get it sir, I'm not using a List, so for what is List? I just want to get the name and post it on the single text view, without the "".

    But still thanks for answering :)

  • CharwakaCharwaka INMember ✭✭✭✭✭
    edited February 2018 Accepted Answer

    @Enohara said:
    @Eric_Lira i dont get it sir, I'm not using a List, so for what is List? I just want to get the name and post it on the single text view, without the "".

    But still thanks for answering :)

    its not about list , first you need traverse through all elements and get data ,so you need list first to store all data, and fetch required value from that list,so use that code it should work .

  • Eric_LiraEric_Lira USMember ✭✭✭
    Accepted Answer

    Hi @Enohara
    I just showed you as a List because you attached an image showing a xml with a list, I assumed that is what your API returns. If your API can return just one item is basically the same idea, you can just deserialize from json straight to your object, but you need a class representing this object anyway.

    Cheers!

  • Xammy10Xammy10 Member ✭✭✭

    I get i now, the List is for my controller, Thank you so much @Eric_Lira and @Charwaka :)

Sign In or Register to comment.