我知道这个问题已被多次询问过。但我仍然是一个新手,似乎我无法反序列化我从get请求获得的json数组。
这是我的代码:
Activity.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);
评论中的代码对我来说也不起作用......
CharchingPointClass.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 ()
{
}
}
更新: JSON
[
{
"_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对象(例如{&#34; name&#34;:&#34; value&#34;})反序列化为类型&#39; iChargeClassTest.CharchingPointClass&#39;因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或者将反序列化类型更改为实现集合接口(例如ICollection,IList)的数组或类型,例如可以从中反序列化的List JSON array.JsonObjectAttribute也可以添加到类型中以强制它从JSON数组反序列化。}
有人可以帮我解决这个问题吗?
非常感谢帮助:)
答案 0 :(得分:2)
尝试使用以下类来序列化。
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)
答案 1 :(得分:1)
您必须使用该行进行反序列化。
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; }
}