我知道这个问题已经被问过很多次了。但我仍然是新手,似乎我无法反序列化我从 get 请求中获得的 json 数组。
这是我的代码:
活动.cs
var getResponse = RequestClass.GetChargingPointsData(pathUrl, currentToken);
System.Diagnostics.Debug.WriteLine("My GETresponse: " + getResponse);
//CharchingPointClass getCPDetail = JsonConvert.DeserializeObject<CharchingPointClass> (getResponse);
//var getCPDetail = JsonConvert.DeserializeObject<CharchingPointClass> (getResponse);
//List<CharchingPointClass> getCPDetail = (List<CharchingPointClass>)JsonConvert.DeserializeObject(getResponse, typeof(List<CharchingPointClass>));
CharchingPointClass result = (CharchingPointClass)JsonConvert.DeserializeObject(getResponse, typeof(CharchingPointClass));
System.Diagnostics.Debug.WriteLine("My longitudes: " + result.lat);
评论中的代码也对我不起作用...
查清点类.cs
public class CharchingPointClass
{
public string _id { get; set; }
public double price { get; set; }
public string type { get; set; }
public string model { get; set; }
public string modelID { get; set; }
public double lat { get; set; }
public double lng { get; set; }
public string address { get; set; }
public int __v { get; set; }
public string modified_at { get; set; }
public string created_at { get; set; }
public CharchingPointClass ()
{
}
}
更新:杰森
[
{
"_id": "56d98506a7012ee0001bc42c",
"price": 135.5,
"type": "Type2",
"model": "id11",
"modelID": "Model1",
"lat": 15.5,
"long": 123.56,
"address": "Van Vaerenberghstraat 11, 2600 Berchem",
"__v": 0,
"modified_at": "2016-03-04T15:58:44.142Z",
"created_at": "2016-03-04T12:52:22.719Z"
},
{
"_id": "56d98909a7012ee0001bc42d",
"price": 5000,
"type": "TypeBart",
"model": "MijnModel",
"modelID": "Home-1-ABC",
"lat": 4.427484,
"long": 51.197772,
"address": "Van Vaerenberghstraat 11, 2600 Berchem",
"__v": 0,
"modified_at": "2016-03-04T13:09:29.173Z",
"created_at": "2016-03-04T13:09:29.172Z"
},
{
"_id": "56d98987a7012ee0001bc42e",
"price": 22.22,
"type": "Type222",
"model": "aaa",
"modelID": "abc1223456",
"lat": 4.442229,
"long": 51.141699,
"address": "Veldkant 37, 2550 Kontich",
"__v": 0,
"modified_at": "2016-03-04T13:11:35.466Z",
"created_at": "2016-03-04T13:11:35.466Z"
},
{
"_id": "56d989d0a7012ee0001bc42f",
"price": 0.17,
"type": "TypeNG",
"model": "ModelDeborah",
"modelID": "ModelIDDeb",
"lat": 4.418491,
"long": 51.222212,
"address": "Osystraat 53, 2060 Antwerpen",
"__v": 0,
"modified_at": "2016-03-04T13:12:48.706Z",
"created_at": "2016-03-04T13:12:48.706Z"
}
]
错误信息:
{Newtonsoft.Json.JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型"iChargeClassTest.CharchingPointClass",因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3]),或者将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化的列表。JsonObjectAttribute 也可以添加到类型中,以强制它从 JSON 数组反序列化。
有人可以帮助我摆脱困境吗?
非常感谢:)
尝试使用以下类来改用序列化。
public class CarchingPointClass
{
public string _id { get; set; }
public double price { get; set; }
public string type { get; set; }
public string model { get; set; }
public string modelID { get; set; }
public double lat { get; set; }
public double @long { get; set; }
public string address { get; set; }
public int __v { get; set; }
public string modified_at { get; set; }
public string created_at { get; set; }
}
然后使用以下行进行序列化。
var result=JsonConvert.DeserializeObject<List<CarchingPointClass>>(inputString)
您必须使用该行进行反序列化。
var result=JsonConvert.DeserializeObject<List<CarchingPointClass>>(inputString)
类将是
public class CarchingPointClass
{
public string _id { get; set; }
public double price { get; set; }
public string type { get; set; }
public string model { get; set; }
public string modelID { get; set; }
public double lat { get; set; }
[JsonProperty("long")]
public double lng { get; set; }
public string address { get; set; }
public int __v { get; set; }
public string modified_at { get; set; }
public string created_at { get; set; }
}