我正在嘗試反序列化來自 js 代碼的 json 字串,資料包含我想要反序列化的 json 字串。
即將到來的字串是:
{"metalRingID":"FB11111","birdData":[{"longitude":-3.0851070084624497,"latitude":51.02832183751735,"gridRef":"ST2426","date":"2020-01-05T00:00:00"},{"longitude":-2.233409881591797,"latitude":51.5276985168457,"gridRef":null,"date":"2020-01-02T00:00:00"},{"longitude":-2.3790299892425537,"latitude":51.4547004699707,"gridRef":null,"date":"2020-01-03T00:00:00"},{"longitude":-1.6884700059890747,"latitude":51.68299865722656,"gridRef":null,"date":"2020-01-05T00:00:00"}]}
這是保存該資料的模型:
using System;
using Newtonsoft.Json;
namespace BirdProject.Model.ViewModel
{
public class birdDataSolutionVM
{
[JsonProperty("metalRingID")]
public string metalRingID;
[JsonProperty("birdData")]
public List<birdRecordVM> birdData;
}
}
這是應該完成這項作業的生產線。
var birdRecords = JsonConvert.DeserializeObject<List<birdDataSolutionVM>>(data);
我收到的錯誤是下一個:
無法將當前 JSON 物件(例如 {"name":"value"})反序列化為型別 'System.Collections.Generic.List`1[BirdProject.Model.ViewModel.birdDataSolutionVM]' 因為該型別需要 JSON 陣列(例如 [ 1,2,3]) 以正確反序列化。\n要修復此錯誤,請將 JSON 更改為 JSON 陣列(例如 [1,2,3])或將反序列化型別更改為正常的 .NET 型別(例如不是像整數這樣的原始型別,不是像陣列或串列這樣的集合型別)可以從 JSON 物件反序列化。JsonObjectAttribute 也可以添加到該型別以強制它從 JSON 物件反序列化。\n路徑“metalRingID”,第 1 行,位置 15。
uj5u.com熱心網友回復:
您應該反序列化為型別,而不是型別的陣列。它是包含陣列的單個物件,但根 json 不是陣列。
var birdRecords = JsonConvert.DeserializeObject<birdDataSolutionVM>(data);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/497237.html
上一篇:JQ字串不能是csv格式的
