无法将 Json 反序列化为数据集或数据表
Unable to Deserialize Json to Dataset or Datatable
我从 http get 收到 JSON 响应,但我似乎无法反序列化它。这是获取 json.
的代码
HttpClient client = new();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {AuthToken}");
HttpContent result = client.GetAsync(addressUrl).Result.Content;
var jsonString = await result.ReadAsStringAsync();
我尝试了几种反序列化方法:
var table = JsonConvert.DeserializeObject<DataSet>(jsonString);
给我:Newtonsoft.Json.JsonSerializationException:'Unexpected JSON token when reading DataTable. Expected StartArray, got String. Path '['@odata.context']',第 1 行,位置 129。'
DataTable dt = (DataTable)JsonConvert.DeserializeObject(jsonString, (typeof(DataTable)));
给我:Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.'
我在这里看了一堆回复,其中大部分都说 JSON 格式不正确。我将 JSON 粘贴到 jsonlint 中,它说格式正确。这是我拥有的:
{
"@odata.context": "https://<server>/odataservice/odata/view/$metadata#reason_code",
"value": [{
"id": "10",
"reason": "Leasehold Improvements",
"company_no": "123"
}, {
"id": "11",
"reason": "Promotional Expense",
"company_no": "123"
}, {
"id": "12",
"reason": "Display Expense",
"company_no": "123"
}, {
"id": "9",
"reason": "Golf Tournament",
"company_no": "123"
}]
}
您的 json 包含一个 collection 和一个 header。您只能将 collection(值)转换为数据 table。所以试试这个
var jsonParsed=JObject.Parse(jsonString);
DataTable dataTable = jsonParsed["value"].ToObject<DataTable>();
我从 http get 收到 JSON 响应,但我似乎无法反序列化它。这是获取 json.
的代码 HttpClient client = new();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {AuthToken}");
HttpContent result = client.GetAsync(addressUrl).Result.Content;
var jsonString = await result.ReadAsStringAsync();
我尝试了几种反序列化方法:
var table = JsonConvert.DeserializeObject<DataSet>(jsonString);
给我:Newtonsoft.Json.JsonSerializationException:'Unexpected JSON token when reading DataTable. Expected StartArray, got String. Path '['@odata.context']',第 1 行,位置 129。'
DataTable dt = (DataTable)JsonConvert.DeserializeObject(jsonString, (typeof(DataTable)));
给我:Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.'
我在这里看了一堆回复,其中大部分都说 JSON 格式不正确。我将 JSON 粘贴到 jsonlint 中,它说格式正确。这是我拥有的:
{
"@odata.context": "https://<server>/odataservice/odata/view/$metadata#reason_code",
"value": [{
"id": "10",
"reason": "Leasehold Improvements",
"company_no": "123"
}, {
"id": "11",
"reason": "Promotional Expense",
"company_no": "123"
}, {
"id": "12",
"reason": "Display Expense",
"company_no": "123"
}, {
"id": "9",
"reason": "Golf Tournament",
"company_no": "123"
}]
}
您的 json 包含一个 collection 和一个 header。您只能将 collection(值)转换为数据 table。所以试试这个
var jsonParsed=JObject.Parse(jsonString);
DataTable dataTable = jsonParsed["value"].ToObject<DataTable>();