I am attempting to deserialize a piece of JSON with a specific structure like so:
{
"label1": "value1",
"label2": [
[
[
"concept_id_1",
"concept_1"
],
score_1
],
[
[
"concept_id_2",
"concept_2"
],
score_2
],
……
],
"label3": "value3",
"label4": "value4"
}
For what it's worth, the scores are floats and everything else is a string. The number of returned concepts under "label2" is variable.
I'm attempting to deserialize it using JSON.net. The only content I actually care about is the inside nest of arrays labelled "label2", however the lack of labels inside the arrays is blocking me at every turn.
I've tried a variety of approaches, but the most successful so far seems to be this:
public class Parsed_JSON {
public string label1 { get; set; }
public ICollection<Full_Result> label2 { get; set; }
public string label3 { get; set; }
public string label4 { get; set; }
}
public class Full_Result {
public IList<string> values { get; set; }
public float score { get; set; }
}
Parsed_JSON result = JsonConvert.DeserializeObject<Parsed_JSON>(JSON);
However this is failing with the error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JSON_Parsing+Full_Result' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Ultimately I'll be looking to iterate through the contents of label2 so that I can build a DataTable of them like so:
concept_id_1 concept_1 score_1
concept_id_2 concept_2 score_2
How can I deserialize this JSON?