【问题标题】:JsonConvert.DeserializeObject not working sometimesJsonConvert.DeserializeObject 有时不工作
【发布时间】:2017-05-16 15:58:16
【问题描述】:

我正在尝试使用 JsonConver.DeserializeObject 反序列化一些 json。但是它不适用于我正在使用的api中的一些json。这是一个不起作用的链接:https://api.pokemontcg.io/v1/cards?setCode=smp

这是一个有效的链接。 https://api.pokemontcg.io/v1/cards?setCode=sm2

我不确定为什么它有时有效,有时无效。从中出来的数据非常相似,只是不同的卡片。 代码如下:

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

现在我的卡片课

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

    }
}

有根

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

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

如果你能帮助我,我会觉得很棒。

【问题讨论】:

  • 当它失败时,它会给你一个异常或错误信息吗?
  • @Jason 不,它没有,它只是让我的 jsonObject 为空。
  • 我建议您仔细比较一个工作结果和一个非工作结果,并找出不同之处。
  • 我会将 json 反序列化的内容记录为空对象

标签: c# android json xamarin xamarin.android


【解决方案1】:

我已经测试了你的 json。

这是模型

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

    }
}

这就是我所说的反序列化

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

(NB WRONG_JSON 是你的 json 字符串...)

【讨论】:

  • 我看到了我的问题。我用一个 int 作为数字。虽然它应该是一个字符串。这个名字欺骗了我。
  • 可以跳过Model中一些不必要的属性吗?它能够反序列化 json 吗?
  • 你可以选择你想要反序列化的内容。通常 JsonIgnore 属性是一个很好的解决方案
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
相关资源
最近更新 更多