Reputation: 321
I am getting this error when trying to read JSON from a stream:
Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable. Expected StartArray, got String. Path 'Data.GUID', line 1, position 110.'
Error is on the line syncResult = serializer.Deserialize<cls_Sync.SyncResult>(json)
cls_Sync.SyncResult syncResult = new cls_Sync.SyncResult();
using (var stream = await response.Content.ReadAsStreamAsync())
{
using (var reader = new StreamReader(stream))
{
//reader.ReadToEnd();
using (var json = new JsonTextReader(reader))
{
syncResult = serializer.Deserialize<cls_Sync.SyncResult>(json);
}
}
}
This is the structure of the class:
public class SyncResult
{
public Guid GUID;
public bool IsSuccessful;
public string Message;
public string SQL;
public DataSet Data;
public List<Guid> SyncResultGUIDs;
public Guid LastSyncGUID;
public SyncResult();
}
Sample JSON:
{
"ContentEncoding": null,
"ContentType": "Application/json",
"Data": {
"GUID": "00000000-0000-0000-0000-000000000000",
"IsSuccessful": true,
"Message": null,
"SQL": null,
"Data": {
"Table": []
},
"SyncResultGUIDs": [],
"LastSyncGUID": "00000000-0000-0000-0000-000000000000"
},
"JsonRequestBehavior": 0,
"MaxJsonLength": null,
"RecursionLimit": null
}
Upvotes: 0
Views: 206
Reputation: 129707
The problem is you are missing an outer class. You are attempting to deserialize into a SyncResult
, but that object is actually one level further down in the JSON.
Try defining the following wrapper class:
public class ResultWrapper
{
public string ContentEncoding { get; set; }
public string ContentType { get; set; }
public SyncResult Data { get; set; }
public long JsonRequestBehavior { get; set; }
public int? MaxJsonLength { get; set; }
public int? RecursionLimit { get; set; }
}
Then deserialize into the wrapper and get the SyncResult
from that:
syncResult = serializer.Deserialize<ResultWrapper>(json).Data;
Fiddle: https://dotnetfiddle.net/0uY9Yo
Note: you can omit any properties from the wrapper that you do not actually need. If all you need is the SyncResult
then the wrapper could be as simple as this:
public class ResultWrapper
{
public SyncResult Data { get; set; }
}
Upvotes: 2