简体   繁体   English

JsonConvert.DeserializeObject有时无法正常工作

[英]JsonConvert.DeserializeObject not working sometimes

I'm trying to Deserialize some json using JsonConver.DeserializeObject . 我正在尝试使用JsonConver.DeserializeObject反序列化一些json。 however it's not working on some json from the api I'm using. 但是它不适用于我正在使用的api上的某些json。 here is a link that does not work: https://api.pokemontcg.io/v1/cards?setCode=smp 这是无效的链接: https : //api.pokemontcg.io/v1/cards?setCode=smp

and here is a link that does work. 这是一个有效的链接。 https://api.pokemontcg.io/v1/cards?setCode=sm2 https://api.pokemontcg.io/v1/cards?setCode=sm2

I'm not sure why it sometimes works and sometimes not. 我不确定为什么有时会起作用,有时却不起作用。 The data that comes out of it is very similar to each other, just different cards. 产生的数据彼此非常相似,只是不同的卡。 Here is the code: 这是代码:

public static async Task<T> GetDataAsync<T>(this HttpClient client, string address, string querystring)
            where T : class
        {
            var uri = address;

            if (!string.IsNullOrEmpty(querystring))
            {
                uri += querystring;
            }

            var httpMessage = await client.GetStringAsync(uri);
            var jsonObject = JsonConvert.DeserializeObject<T>(httpMessage);

            return jsonObject;
        }

Now my card class 现在我的卡类

namespace CardAppReal.Lib.Models
{
    public class Card
    {
        public string id { get; set; }
        public string name { get; set; }
        public string imageUrl { get; set; }
        public string imageUrlHiRes { get; set; }
        public string supertype { get; set; }   // if pokemon, trainer or energy
        public string setcode { get; set; }
        public int number { get; set; }
        public string set { get; set; }

    }
}

With a root 有根

using System.Collections.Generic;
using CardAppReal.Lib.Models;

namespace CardAppReal.Lib.Entities
{
    public class RootCard
    {
        public List<Card> cards { get; set; }
    }
}

If u could help me out I would find it amazing. 如果您能帮助我,我会发现它很棒。

I have tested your json. 我已经测试过您的json。

this is the Model 这是模型

namespace Test
{
public class Attack
    {
        public List<string> cost { get; set; }
        public string name { get; set; }
        public string text { get; set; }
        public string damage { get; set; }
        public int convertedEnergyCost { get; set; }
    }

    public class Weakness
    {
        public string type { get; set; }
        public string value { get; set; }
    }

    public class Resistance
    {
        public string type { get; set; }
        public string value { get; set; }
    }

    public class Ability
    {
        public string name { get; set; }
        public string text { get; set; }
        public string type { get; set; }
    }

    public class Card
    {
        public string id { get; set; }
        public string name { get; set; }
        public int nationalPokedexNumber { get; set; }
        public string imageUrl { get; set; }
        public string imageUrlHiRes { get; set; }
        public string subtype { get; set; }
        public string supertype { get; set; }
        public string hp { get; set; }
        public List<string> retreatCost { get; set; }
        public string number { get; set; }
        public string artist { get; set; }
        public string rarity { get; set; }
        public string series { get; set; }
        public string set { get; set; }
        public string setCode { get; set; }
        public List<string> types { get; set; }
        public List<Attack> attacks { get; set; }
        public List<Weakness> weaknesses { get; set; }
        public List<Resistance> resistances { get; set; }
        public string evolvesFrom { get; set; }
        public Ability ability { get; set; }
        public List<string> text { get; set; }
    }

    public class RootObject
    {
        public List<Card> cards { get; set; }

    }
}

And this is how I call the Deserialize 这就是我所说的反序列化

RootObject root = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(RootObject.WRONG_JSON);

(NB WRONG_JSON is your json string...) (注意WRONG_JSON是您的json字符串...)

这是结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM