I have the following json string:
private const string Json = @"{
'responseMessage': '',
'response': 'PASS',
'responseData': {
'user': {
'initials': 'XXX',
'name': 'XXXX XXXX',
'companies': [
'0002',
'0007',
'0022',
'0033',
'9999'
],
'employee': 'XXXX'
}
},
'responseFor': 'myCommand'
}'";
My ServerResponse class looks like this:
[JsonObject(MemberSerialization.OptIn)]
public class ServerResponse
{
[JsonProperty("responseFor")]
public string ResponseFor { get; set; }
[JsonProperty("responseData")]
public string ResponseData { get; set; }
[JsonProperty("response")]
public string Response { get; set; }
[JsonProperty("responseMessage")]
public string ResponseMessage { get; set; }
}
I call this command:
var serverResponse = JsonConvert.DeserializeObject<ServerResponse>(Json);
I would expect that this would fill in my serverResponse object but instead I get this exception:
{Newtonsoft.Json.JsonReaderException: Error reading string. Unexpected token: StartObject. Path 'responseData', line 4, position 30.
at Newtonsoft.Json.JsonReader.ReadAsStringInternal () [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonTextReader.ReadAsString () [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonContract contract, Boolean hasConverter) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) [0x00000] in <filename unknown>:0 }
What am I doing wrong here??
responseDatais not a string, it's an object with several fields.