Can't deserialize json array

2019-09-10 01:51发布

I know this question has been asked many times. But i'm still a newbie and it seems like I can't deserialize the json array I got from a get request.

This is my code:

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);

The codes in comment also didn't work for me...

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 ()
    {
    }
}

UPDATE: 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"
}

]

Error message:

{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'iChargeClassTest.CharchingPointClass' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from JSON array.JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON array.}

Can anybody help me out of this please?

Help is very appreciated :)

2条回答
姐就是有狂的资本
2楼-- · 2019-09-10 02:49

Try using the following class to serialize to instead.

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; }
}

Then serialize using the following line.

var result=JsonConvert.DeserializeObject<List<CarchingPointClass>>(inputString)
查看更多
Fickle 薄情
3楼-- · 2019-09-10 02:51

You have to use the line for Deserializing.

  var result=JsonConvert.DeserializeObject<List<CarchingPointClass>>(inputString)

Class will be

  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; }
 }
查看更多
登录 后发表回答