2

I am using .NET 4.7, C#, JSON.NET, MVC 5

I have some input JSON:

[
  {
     "id" : 1
  },
  {
     "id" : 2
  },
  {
     "id" : 3
  }
]

This is provided by a 3rd party API

Normally I would use code such as, to deserialize:

var content = await response.Content.ReadAsStringAsync();
JObject json = JObject.Parse(content);
orderList = JsonConvert.DeserializeObject<OrderList>(json.ToString());

However I am finding that:

JObject json = JObject.Parse(content);

Crashes out with the JSON in question. I strongly suspect that the surrounding "[" and "]" is the cause.

I would normally add :

{
  items: [

to correct the input JSON.

Is there a better way, to deserialize it, as it seems this input JSON is incomplete although when I test it, it does seem to be valid JSON.

Possibly I should be using JArray instead of JObject?

Thanks in advance.

SamJolly
  • 6,347
  • 13
  • 59
  • 125
  • 1
    `Normally I would use code such as, to deserialize: var content = await response.Content.ReadAsStringAsync(); JObject json = JObject.Parse(content); orderList = JsonConvert.DeserializeObject(json.ToString());` hehe it is redundant ... why not `var content = await response.Content.ReadAsStringAsync(); orderList = JsonConvert.DeserializeObject(content);` – Selvin Aug 29 '19 at 10:46
  • 1
    Your JSON is invalid. – Mahdi Aug 29 '19 at 10:46
  • 1
    @Mahdis I think missing commas are just typo in the question – Selvin Aug 29 '19 at 10:47
  • 1
    @Selvin, yes probably, but he needs to fix it anyway. – Mahdi Aug 29 '19 at 10:48
  • Commas added, it was a typo. Thanks for the spot however. – SamJolly Aug 29 '19 at 10:52
  • 2
    Either deserialise to a list of a concrete class (the best way) or use `JArray.Parse` instead. – DavidG Aug 29 '19 at 10:52
  • @Mahdi interesting that you think it is invalid. Was your concern about the missing commas or that you cannot have wrapping "[" and "]"? – SamJolly Aug 29 '19 at 10:54
  • 1
    @SamJolly just the commas. The outer brackets are fine. – Mahdi Aug 29 '19 at 11:00

2 Answers2

3

You are missing commas "," in the JSON. It should be like this:

[
  {
     "id" : 1
  },
  {
     "id" : 2
  },
  {
     "id" : 3
  }
]

And you can deserialize it like that:

var content = await response.Content.ReadAsStringAsync();
var list = JsonConvert.DeserializeObject<List<MyClass>>(content);

Where list is a List<MyClass>

public class MyClass
{
    public int id { get; set; }
}
G.Dimov
  • 2,173
  • 3
  • 15
  • 40
1

You have 3 options:

  1. Instead of JObject.Parse, use JArray.Parse because the JSON is an array, not an object. This is not the best way to achieve what you want.

  2. Deserialise to a List<T> where T is a concrete class that contains the matching properties of the object. for example:

    var result = JsonConvert.DeserializeObject<List<Order>>(json);
    

    This is better, but...

  3. The best option is not to read the HttpClient response as a string in the first place and let the framework do the work for you. You should use the ReadAsAsync<T> extension method. Internally, this uses a stream and is more efficient than first going to string. So for example:

    List<Order> orders = await response.Content.ReadAsAsync<List<Order>>();
    
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • thank you for this, however I am not seeing ReadAsAsync as an available option. I can see ReadAsStreamAsync and ReadAsStringAsync. Also I have "using System.Net.Http;" Thoughts? – SamJolly Aug 29 '19 at 11:43
  • Used this: https://stackoverflow.com/questions/10399324/where-is-httpcontent-readasasync – SamJolly Aug 29 '19 at 11:53
  • 1
    Sorry, I thought linking to the docs would be enough to figure it out. That question is also useful. – DavidG Aug 29 '19 at 12:21