Parse Json response in xamarin.Android

I am using JSON.NET(Newtonsoft.Json NuGet) in xamarin.Android to parse a response from an API. My response is like this:

[
  {
    "id": "21",
    "date": "2018-01-01",
    "name": "Name1"
  },
  {
    "id": "22",
    "date": "2018-01-02",
    "name": "Name2",
  }
]

As you see, it contains 2 id tags. The numer of id-tags can change. Sometimes the id-value is 21 but sometimes 48 or other... So i want to get the value of each id, date and name tag.
I tryed with an for each loop but don't succeed...

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I have never used it in a Android app but I think it's the same that in a XF app.

    If you take a look to newtonsoft site:

    string json = @{
    'Name': 'Bad Boys',
    'ReleaseDate': '1995-4-7T00:00:00',
    'Genres': [
    'Action',
    'Comedy'
    ]
    }";

    Movie m = JsonConvert.DeserializeObject(json);

    string name = m.Name;

    So you should do something like

    List m = JsonConvert.DeserializeObject(json);

    where "mymodel" is something like

    string id {get;set}
    string date {get;set;}
    string name {get;set;}
  • JonasPTFLJonasPTFL Member ✭✭

    @AlessandroCaliaro said:
    I have never used it in a Android app but I think it's the same that in a XF app.

    If you take a look to newtonsoft site:

    string json = @{
    'Name': 'Bad Boys',
    'ReleaseDate': '1995-4-7T00:00:00',
    'Genres': [
    'Action',
    'Comedy'
    ]
    }";

    Movie m = JsonConvert.DeserializeObject(json);

    string name = m.Name;

    So you should do something like

    List m = JsonConvert.DeserializeObject(json);

    where "mymodel" is something like

    string id {get;set}
    string date {get;set;}
    string name {get;set;}

    And how can I use the data from the list?

Sign In or Register to comment.