2

I am trying to deserialize a JSON string, where that JSON string has a "events" array, the problem lies in that when I try to dererialize the string, as seeing below, it is trying to find the "field_name" variable however not all objects in the array is built in the same way, only some of them have a "field_name" variable. I don't have control over the API in question.

What basically I am trying to do

dynamic obj = JsonConvert.DeserializeObject(json);
var audits = obj.audits;
List<dynamic> dynamics = JsonConvert.DeserializeObject<List<dynamic>>
(audits.ToString());
List<Event> events = JsonConvert.DeserializeObject<List<Event>>
 (dynamics[0].events.ToString());

//The Event class
public class Event
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("value")]
    public string Value { get; set; }
    [JsonProperty("field_name")]
    public string Field_Name { get; set; }

}

I continue to get this exception:

Newtonsoft.Json.JsonReaderException: Error reading string. Unexpected token: 
StartArray. Path '[5].value', line 44, position 15.

JSON array something like this:

[{
   "id":"id",
   "field_name":"field_name",
   "something":"something"
},{
   "id":"id",
   "number":"number",
   "something":"something"
},{
   "id":"id",
   "field_name":"field_name",
   "something":"something"
},{
   "id":"id",
   "name":"name",
   "something":"something"
}]
  • Missing value generally shouldn't be a problem for the library you are using. Can you show us what does the line 44 look like? It seems either your Event model does not properly reflect the structure of the JSON or the your JSON string is not formatted correctly. – Milos Mrdovic Dec 07 '17 at 12:58
  • ```dynamics[0].events``` is not defined in the JSON you provided. I get a RuntimeBinderException (NULL -Exception). Maybe you can Null-check it. or provide some code (say, a unit test), which actually reproduces your problem. – FrankM Dec 07 '17 at 13:03
  • One time i got this problem and after search a lot and not find anything, i used foreach to create my object. – Felipe Deveza Dec 07 '17 at 13:07
  • @MilosMrdovic The line number can change but now I actually think I found the problem, it seems like that the "value" property can be both a string and an array, what would be the best practice to handle this kind of error?. – codingmonkey Dec 07 '17 at 13:43
  • @codingmonkey Right. I think you'll find a an answer here https://stackoverflow.com/questions/35978392/deserialize-json-as-object-or-array-with-json-net or here https://stackoverflow.com/questions/29688498/how-to-deserialize-json-to-objects-of-the-correct-type-without-having-to-define – Milos Mrdovic Dec 07 '17 at 13:48
  • @codingmonkey See [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/10263) – Brian Rogers Dec 07 '17 at 23:26
  • Thanks a lot guys, made it work! – codingmonkey Dec 08 '17 at 12:21

1 Answers1

0

You can use ObjectMapper as below:

private ObjectMapper objectMapper = new ObjectMapper();

List<Event> events = readValue(dynamics[0].events.ToString(), new TypeReference<List<Event>>() {});
Sanket Patel
  • 227
  • 1
  • 14