0

Here is my code:

private void OnIncomingData(string data)
{
    Console.WriteLine("RECEIVED: "  + data);
    TransferData transfer = JsonConvert.DeserializeObject<TransferData>(data);
}

I receive an error:

JsonReaderException: Unexpected character encountered while parsing value: {. Path 'data', line 1, position 26.

Here is what my Console.WriteLine gives:

RECEIVED: {"header":"0x001","data":{"connectionId":"85"},"connectionId":85}

Here is my TransferData class:

public class TransferData
{
    public string header;
    public string data;
    public int connectionId;
}

As you can see the json string is correct. Why than I receive this error? How can I fix it?

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • 3
    `data` in the json doesn't look rights. It's not a string, it's a object with a `connectionId` property. – Sean Apr 02 '20 at 10:26

2 Answers2

1

Your type says that Data is a string, but it looks like an object to me. Think you need something like:

public class TransferData
{
    public string header;
    public Data data;
    public int connectionId;
}

public class Data
{
    public int connectionId;
}
tigerswithguitars
  • 2,497
  • 1
  • 31
  • 53
0

Your model does not correspond to JSON schema as in JSON you have an object data while in model it's string. Either wrap the object in JSON is string, or change data type either to type describing what you can receive, or to JObject