无法反序列化json数组 [英] Can't deserialize json array

查看:397
本文介绍了无法反序列化json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过很多次了.但是我仍然是一个新手,似乎我无法反序列化从get请求获得的json数组.

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.

这是我的代码:

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

更新: json

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

]

错误消息:

{Newtonsoft.Json.JsonSerializationException:无法将当前JSON对象(例如{"name":"value"})反序列化为类型'iChargeClassTest.CharchingPointClass',因为该类型需要JSON数组(例如[1,2,3 ])正确反序列化. 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如可以从中进行反序列化的List JSON array.JsonObjectAttribute也可以添加到类型中,以强制其从JSON数组反序列化.}

{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.}

有人可以帮我吗?

非常感谢您的帮助:)

推荐答案

您必须使用该行进行反序列化.

You have to use the line for Deserializing.

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

这篇关于无法反序列化json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆