0

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??

4
  • 3
    responseData is not a string, it's an object with several fields. Commented Feb 19, 2015 at 22:44
  • What would I define ResponseData as in my ServerResponse class? I already tried List<string> ResponseData. Commented Feb 19, 2015 at 22:47
  • It's not a list of strings. It's an object. With fields. As I stated. Commented Feb 19, 2015 at 22:48
  • I am presently doing something like this: var jsonServerResponse = JObject.Parse(response); which works it just feels ugly because I need to do things like var responseData = JObject.Parse(jsonServerResponse["responseData"].ToString()); Commented Feb 19, 2015 at 22:55

1 Answer 1

1

You need a class for ResponseData in order for the serialization to work, as the value for it in your json is not a string. You'll also need one for User.

Create response data and user classes that looks something like this.

[JsonObject(MemberSerialization.OptIn)] public class User { [JsonProperty("initials")] public string Initials { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("companies")] public string[] Companies { get; set; } [JsonProperty("responseMessage")] public string ResponseMessage { get; set; } } [JsonObject(MemberSerialization.OptIn)] public class ResponseData { [JsonProperty("user")] public User User { get; set; } [JsonProperty("employee")] public string Employee { get; set; } } 

Then in your ServerResponse class, change ResponseData to use this ResponseData class.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.