1

Client application I don't have much control over has an established pattern for making calls to a particular API. Drilling down into the existing methods I find it's using System.Net.Http.Json and calling response.Content.ReadFromJsonAsync<TResponse>(content), which I believe just takes a a target type TResponse and tries to convert the passed JSON string accordingly.

I've gotten this to work great in every scenario except one - where the response from the API is an unlabeled array:

[{value: "stuff1"}, {value: "stuff2"}, {value: "stuff3"}] 

Every other JSON response object looks something like this:

{
    data: ["blah", "blah", "blah"]
    value: 15, 
    prop: "whatever"
}

And example working Type:

[JsonPropertyName("data")]
public IList<string> Data {get;set}

[JsonPropertyName("value")]
public int Value {get;set}

[JsonPropertyName("prop")]
public string Prop {get;set}

But my problem response doesn't have any named keys, it's just an array... so how do I create a Type for an array with no key?

0

1 Answer 1

1

Two days of head scratching and I figure it out moments after posting a question:

Just pass in a list of type as the TResponse type:

 IList<MyCustomType>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.