无法反序列化json字符串c#

Posted

技术标签:

【中文标题】无法反序列化json字符串c#【英文标题】:cant deserialize json string c# 【发布时间】:2021-02-26 08:04:37 【问题描述】:

我从 api 调用成功返回以下 json:

  
   "Done":
      "WebHotspotList":[
         
            "address":"112esBMAPaRTX9fotk74oT6qYnsixPCXW88r1cNeTdRZuzZx2eFu",
            "rewards_total":0.48443026,
            "geo_long_city":"Fort Myers",
            "geo_long_country":"United States",
            "geo_long_state":"Florida",
            "geo_long_street":"Hampton Park Court",
            "geo_short_city":"Fort Myers",
            "geo_short_country":"US",
            "geo_short_state":"FL",
            "block":588158,
            "block_added":584666,
            "location":"8c4413701cdbbff",
            "name":"tame-sable-dinosaur",
            "nonce":1,
            "owner":"12ye1K1sbGVffG1hQdczozS5Ykg6K6ZCLrSJFr4Duv4srP8GgD8",
            "score":0.166534423828125,
            "score_update_height":588112,
            "lat":26.583755808331496,
            "lng":-81.734861907528042,
            "status_gps":null,
            "status_height":588133,
            "status_online":"online",
            "geo_short_street":"Hampton Park Ct",
            "geo_city_id":"Zm9ydCBteWVyc2Zsb3JpZGF1bml0ZWQgc3RhdGVz"
         ,
         
            "address":"11DbWc7WiwzqE6Yf7a6onxTA1oPTTN5XB3H18vazLF6e6qCD2cH",
            "rewards_total":3.62155897,
            "geo_long_city":"Fort Myers",
            "geo_long_country":"United States",
            "geo_long_state":"Florida",
            "geo_long_street":"Shoreside Court",
            "geo_short_city":"Fort Myers",
            "geo_short_country":"US",
            "geo_short_state":"FL",
            "block":588158,
            "block_added":571225,
            "location":"8c441370110d5ff",
            "name":"active-ultraviolet-bison",
            "nonce":3,
            "owner":"12ye1K1sbGVffG1hQdczozS5Ykg6K6ZCLrSJFr4Duv4srP8GgD8",
            "score":0.13067626953125,
            "score_update_height":588158,
            "lat":26.584657124799811,
            "lng":-81.741675006677724,
            "status_gps":null,
            "status_height":588103,
            "status_online":"online",
            "geo_short_street":"Shoreside Ct",
            "geo_city_id":"Zm9ydCBteWVyc2Zsb3JpZGF1bml0ZWQgc3RhdGVz"
         ,
         
            "address":"1129ENpMcxLZuskVDNeog4WwRjpCAn9Nz2fQwuyxU3iVXbTgA3xY",
            "rewards_total":82.23821902,
            "geo_long_city":"Cape Coral",
            "geo_long_country":"United States",
            "geo_long_state":"Florida",
            "geo_long_street":"Southwest 9th Avenue",
            "geo_short_city":"Cape Coral",
            "geo_short_country":"US",
            "geo_short_state":"FL",
            "block":588158,
            "block_added":531703,
            "location":"8c441338085cdff",
            "name":"curved-tangelo-worm",
            "nonce":1,
            "owner":"12ye1K1sbGVffG1hQdczozS5Ykg6K6ZCLrSJFr4Duv4srP8GgD8",
            "score":0.407379150390625,
            "score_update_height":588118,
            "lat":26.635174041942353,
            "lng":-81.994893441775318,
            "status_gps":null,
            "status_height":588136,
            "status_online":"online",
            "geo_short_street":"SW 9th Ave",
            "geo_city_id":"Y2FwZSBjb3JhbGZsb3JpZGF1bml0ZWQgc3RhdGVz"
         ,
         
            "address":"112hXZbzTd5NL8w9tzgxUR1xLT54QMwF8TXf3qC8MnYcPBzbRejg",
            "rewards_total":242.20892512,
            "geo_long_city":"San Diego",
            "geo_long_country":"United States",
            "geo_long_state":"California",
            "geo_long_street":"Felspar Street",
            "geo_short_city":"San Diego",
            "geo_short_country":"US",
            "geo_short_state":"CA",
            "block":588158,
            "block_added":531691,
            "location":"8c29a40040045ff",
            "name":"tame-cloud-aphid",
            "nonce":2,
            "owner":"12ye1K1sbGVffG1hQdczozS5Ykg6K6ZCLrSJFr4Duv4srP8GgD8",
            "score":0.3829193115234375,
            "score_update_height":588124,
            "lat":32.802329272122954,
            "lng":-117.23529038681818,
            "status_gps":null,
            "status_height":588095,
            "status_online":"online",
            "geo_short_street":"Felspar St",
            "geo_city_id":"c2FuIGRpZWdvY2FsaWZvcm5pYXVuaXRlZCBzdGF0ZXM"
         ,
         
            "address":"112LCRKPWa8UpG9TwK33mLBQUL8ML2sicbux5ab2bAGJy33NZmm1",
            "rewards_total":5.58308855,
            "geo_long_city":"Orlando",
            "geo_long_country":"United States",
            "geo_long_state":"Florida",
            "geo_long_street":"Tally Ho Avenue",
            "geo_short_city":"Orlando",
            "geo_short_country":"US",
            "geo_short_state":"FL",
            "block":588158,
            "block_added":531684,
            "location":"8c44a908b0c69ff",
            "name":"colossal-mango-caribou",
            "nonce":3,
            "owner":"12ye1K1sbGVffG1hQdczozS5Ykg6K6ZCLrSJFr4Duv4srP8GgD8",
            "score":0.211395263671875,
            "score_update_height":588062,
            "lat":28.577658355985609,
            "lng":-81.205853978803276,
            "status_gps":null,
            "status_height":588143,
            "status_online":"online",
            "geo_short_street":"Tally Ho Ave",
            "geo_city_id":"b3JsYW5kb2Zsb3JpZGF1bml0ZWQgc3RhdGVz"
         ,
         
            "address":"11dvb9sxx3dnxhutSSVcqidYaVMWLaXsc15VWHxeQnKNGv6PAM2",
            "rewards_total":26.15705053,
            "geo_long_city":"Cape Coral",
            "geo_long_country":"United States",
            "geo_long_state":"Florida",
            "geo_long_street":"Southwest 15th Terrace",
            "geo_short_city":"Cape Coral",
            "geo_short_country":"US",
            "geo_short_state":"FL",
            "block":588158,
            "block_added":503866,
            "location":"8c441338592bbff",
            "name":"square-laurel-squid",
            "nonce":2,
            "owner":"12ye1K1sbGVffG1hQdczozS5Ykg6K6ZCLrSJFr4Duv4srP8GgD8",
            "score":0.0079345703125,
            "score_update_height":588155,
            "lat":26.624215777622073,
            "lng":-82.017175824730046,
            "status_gps":null,
            "status_height":588145,
            "status_online":"online",
            "geo_short_street":"SW 15th Terrace",
            "geo_city_id":"Y2FwZSBjb3JhbGZsb3JpZGF1bml0ZWQgc3RhdGVz"
         ,
         
            "address":"112gVjksQAaa2a33eDozxu243RNFTncc9q2xXF2BvwQZcS7nZBKj",
            "rewards_total":157.23587732,
            "geo_long_city":"San Diego",
            "geo_long_country":"United States",
            "geo_long_state":"California",
            "geo_long_street":"Mount La Platta Drive",
            "geo_short_city":"San Diego",
            "geo_short_country":"US",
            "geo_short_state":"CA",
            "block":588158,
            "block_added":472824,
            "location":"8c29a40150043ff",
            "name":"festive-navy-oyster",
            "nonce":2,
            "owner":"12ye1K1sbGVffG1hQdczozS5Ykg6K6ZCLrSJFr4Duv4srP8GgD8",
            "score":0.0290069580078125,
            "score_update_height":588128,
            "lat":32.826945251214731,
            "lng":-117.18764050067978,
            "status_gps":null,
            "status_height":588126,
            "status_online":"online",
            "geo_short_street":"Mt La Platta Dr",
            "geo_city_id":"c2FuIGRpZWdvY2FsaWZvcm5pYXVuaXRlZCBzdGF0ZXM"
         
      ]
   

我无法将结果解析为热点对象的数组/列表。我尝试了以下方法:

var json = response.Content.ReadAsStringAsync().Result;
HotspotArray hotspotArray = JsonConvert
  .DeserializeObject<HotspotArray>(json);
Hotspot[] hotspots = hotspotArray.Hotspots; 
var hotspotNames = "";

 for (int i = 0; i < hotspots.Length; i++)
 
      hotspotNames += hotspots[i].Name + " ";
  

  output.Text = hotspotNames;
  
 


    public class HotspotArray
    
        [JsonProperty("WebHotspotList")]
        public Hotspot[] Hotspots  get; set; 
    

    public class Hotspot
    
        [JsonProperty("address")]
        public string Address  get; set; 

        [JsonProperty("rewards_total")]
        public decimal RewardsTotal  get; set; 

        [JsonProperty("geo_long_city")]
        public string GeoLongCity  get; set; 

        [JsonProperty("geo_long_country")]
        public string GeoLongCountry  get; set; 

        [JsonProperty("geo_long_state")]
        public string GeoLongState  get; set; 

        [JsonProperty("geo_long_street")]
        public string GeoLongStreet  get; set; 

        [JsonProperty("geo_short_city")]
        public string GeoShortCity  get; set; 

        [JsonProperty("geo_short_country")]
        public string GeoShortCountry  get; set; 

        [JsonProperty("geo_short_state")]
        public string GeoShortState  get; set; 

        [JsonProperty("block")]
        public int Block  get; set; 

        [JsonProperty("block_added")]
        public int BlockAdded  get; set; 

        [JsonProperty("location")]
        public string Location  get; set; 

        [JsonProperty("name")]
        public string Name  get; set; 

        [JsonProperty("nonce")]
        public int Nonce  get; set; 

        [JsonProperty("owner")]
        public string Owner  get; set; 

        [JsonProperty("score")]
        public decimal Score  get; set; 

        [JsonProperty("score_update_height")]
        public int ScoreUpdateHeight  get; set; 

        [JsonProperty("lat")]
        public decimal Lat  get; set; 

        [JsonProperty("lng")]
        public decimal Lon  get; set; 

        [JsonProperty("status_gps")]
        public string StatusGPS  get; set; 

        [JsonProperty("status_height")]
        public int StatusHeight  get; set; 

        [JsonProperty("status_online")]
        public string StatusOnline  get; set; 

        [JsonProperty("geo_short_street")]
        public string GeoShortStreet  get; set; 

        [JsonProperty("geo_city_id")]
        public string GeoCityID  get; set; 
    

但是数据不断返回 null...hotspots 数组为 null。我在这里想念什么? TIA

【问题讨论】:

Json root 是“完成”...但它没有在您的模型中处理。 【参考方案1】:

您的 json 中的***类型是带有属性 Done 的类型。 创建类型:

    public class HotspotResponse
    
        public HotspotArray Done  get; set; 
    

并将 json 字符串反序列化到此类中。

【讨论】:

以上是关于无法反序列化json字符串c#的主要内容,如果未能解决你的问题,请参考以下文章

无法反序列化json字符串c#

C# JSON 将文件反序列化为对象列表失败,并将字符串转换为集合错误

无法在 C# 中反序列化 JSON 结果。输入字符串格式不正确错误

Newtonsoft 转义 JSON 字符串无法反序列化为对象

在 Objective C 中反序列化 JSON 字符串

c# 反序列化 Json 字符串