-1

I have this JSON object below

{
  "Metadata": {
    "TotalRecords": 12,
    "CurrentPageSize": 2,
    "CurrentPage": 1,
    "TotalPages": 2
  },
  "Results": [
    {
      "Id": 1,
      "OwnerId": "3be73a87-a977-467a-84c0",
      "OwnerName": "AV",
      "CategoryId": 3,
      "CategoryName": "User 1 Physical",
      "Name": "User 1 Speed",
      "Description": null,
      "NoOfMinutes": 15,
      "CreatedDate": "2019-09-22T03:34:56.4033333",
      "ModifiedDate": null
    },
    {
      "Id": 2,
      "OwnerId": "3be73a87-a977-467a-84c0",
      "OwnerName": "AV",
      "CategoryId": 1,
      "CategoryName": "User 1 Technique",
      "Name": "User 1 tech",
      "Description": null,
      "NoOfMinutes": 60,
      "CreatedDate": "2019-09-22T03:34:56.4033333",
      "ModifiedDate": null
    }
  ]
}

and using JSON2Csharp:

public class Metadata
{
    public int TotalRecords { get; set; }
    public int CurrentPageSize { get; set; }
    public int CurrentPage { get; set; }
    public int TotalPages { get; set; }
}

public class Result
{
    public int Id { get; set; }
    public string OwnerId { get; set; }
    public string OwnerName { get; set; }
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public string Name { get; set; }
    public object Description { get; set; }
    public int NoOfMinutes { get; set; }
    public DateTime CreatedDate { get; set; }
    public object ModifiedDate { get; set; }
}

public class RootObject
{
    public Metadata Metadata { get; set; }
    public List<Result> Results { get; set; }
}

How do mapped this entire thing NOT just Result (refer as Activities) into the Xamarin API service for getting the data and return?

This is the original API before changing the entire structure of the data:

public async List<Activities> GetActivities()
        {
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", "ACCCESS TOKEN HERE");

            var response = httpClient.GetStringAsync("https://XXX.azurewebsites.net/api/v1/Activities");

            JsonConvert.DeserializeObject<Activities>(response);
        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dcpartners
  • 5,176
  • 13
  • 50
  • 73
  • 1
    You are probably looking for the Newtonsoft JSON docs: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm – Cheesebaron Sep 25 '19 at 07:11

1 Answers1

1

What's the return object of your api? If the return object's structure is "Metadata":..., "Results":[...] (the first json in your question), you need to use

JsonConvert.DeserializeObject<RootObject>(response);

to convert it.

Wendee Hsu
  • 98
  • 6
  • Would it convert this the entire structure? "Metadata" and "Results" combined? – dcpartners Sep 25 '19 at 06:47
  • Yes it will. reference: https://www.newtonsoft.com/json/help/html/DeserializeObject.htm – Wendee Hsu Sep 25 '19 at 06:50
  • @dcpartners I'll recommend using `JsonProperty` like the answer here: https://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class. (It's a better way to make sure it converts to your intended type) – Wendee Hsu Sep 25 '19 at 06:54