I have this class:
public class ParseJson
{
private HttpClient _client;
public ParseJson()
{
_client = new HttpClient();
}
public async Task<List<Calibration>> LoadJsonAsync()
{
using (StreamReader r = new StreamReader("Json\\calibration.json"))
{
string responseString = r.ReadToEnd();
Console.WriteLine("responseString: " + responseString);
try
{
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
if ((values["DataType"]).Equals("Calibration_Symptoms"))
{
calibrationClass.CalibrationData(values: values);
}
}
catch (Exception e)
{
Console.WriteLine("Json Exception: " + e);
}
List<Calibration> items = JsonConvert.DeserializeObject<List<Calibration>>(responseString);
return items;
}
}
}
When I run it I get this exception:
Json Exception: Newtonsoft.Json.JsonSerializationException: Error converting value "DataType" to type 'System.Collections.Generic.Dictionary
2[System.String,System.String]'. Path '', line 1, position 10. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.Dictionary
2[System.String,System.String]. at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at HelloWorld.ParseJson.d__2.MoveNext() in C:\Users\User\source\repos\HelloWorld\ParseJson.cs:line 31
I know I can create a model and deserialize to that but ultimately I'd like different Json's to be read in and deserialized.
Is there any way I can deserialize it to Dictionary<string, string>
?
FYI this is the json I'm currently using:
"DataType": "Calibration_Symptoms",
"playerHeight": 1.6739861965179443,
"armSpan": 1.572389006614685,
"calibratedHeadPosition": {
"x": 0.10129322111606598,
"y": 1.6739861965179443,
"z": -0.01975761353969574
},
"symptomSeverity": 0,
"scat5SymptomsQs": [],
"rightHanded": true,
"rightFooted": true,
"testType": false,
"trialSymptomsQs": {
"Gait": [1, 2, 3],
"Balance1": []
}
}