2

Currently the API response includes two top level attributes which are of no use to my need.

a: {
   b: {
      c: [
           data: whichINeed
         ]
      }
   }

If I create models for this I would have unncessary root objects which I want to get rid of ? How can I do this in Refit for Windows App ?

user5381191
  • 611
  • 7
  • 21

2 Answers2

1

You'll need NewtonsoftJsonContentSerializer that exists on package Refit.Newtonsoft.Json.

Then, add it to the RefitSettings:

RestService.For<T>("host"),
  new RefitSettings
  {
    ContentSerializer = new NewtonsoftJsonContentSerializer(jsonSerializerSettings)
  });
Daniel Rusnok
  • 449
  • 1
  • 7
  • 14
0

You can try filtering the JSON string using Newtonsoft.JSON nuget package.

The JSON you provided is incomplete. I assume that this JSON string is an Object and the outermost layer is {}, then we can parse it like this

var obj = JObject.Parse(jsonString);
var c = obj["a"]["b"]["c"];
var myTransferModel = JsonConvert.DeserializeObject<MyModel>(c.ToString());

If the outermost layer is [], please use JArray for parsing.


Update

Refit encapsulating the network request and JSON parsing. You can send a web request directly through HttpClient to get a JSON string.

public async Task<string> GetJsonString(string url)
{
    string result = string.Empty;
    var client = new HttpClient();
    var response = client.GetAsync(url);
    if(response.IsSuccessStatusCode)
    {
        result = await response.Content.ReadAsStringAsync();
    }
    return result;
}

Best regards.

Richard Zhang
  • 7,523
  • 1
  • 7
  • 13
  • I am using [Refit](https://github.com/reactiveui/refit), how would I obtain a Json String ? Am sorry, am new to this I couldnt understand that part. – user5381191 Nov 25 '19 at 09:22