C# JsonConvert.DeserializeObject有时不工作
我正在尝试使用C# JsonConvert.DeserializeObject有时不工作,c#,android,json,xamarin,xamarin.android,C#,Android,Json,Xamarin,Xamarin.android,我正在尝试使用JsonConver.DeserializeObject反序列化一些json。但是,我使用的api中的一些json不起作用。以下是一个不起作用的链接: 这里有一个链接确实有效 我不知道为什么它有时有效,有时无效。从中得到的数据彼此非常相似,只是不同的卡片。 代码如下: public static async Task<T> GetDataAsync<T>(this HttpClient client, string address, string query
JsonConver.DeserializeObject
反序列化一些json。但是,我使用的api中的一些json不起作用。以下是一个不起作用的链接:
这里有一个链接确实有效
我不知道为什么它有时有效,有时无效。从中得到的数据彼此非常相似,只是不同的卡片。
代码如下:
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;
}
有根
using System.Collections.Generic;
using CardAppReal.Lib.Models;
namespace CardAppReal.Lib.Entities
{
public class RootCard
{
public List<Card> cards { get; set; }
}
}
使用System.Collections.Generic;
使用CardAppReal.Lib.Models;
名称空间CardAppReal.Lib.Entities
{
公共类根卡
{
公共列表卡{get;set;}
}
}
如果你能帮我,我会觉得很神奇。我已经测试了你的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; }
}
}
名称空间测试
{
公开类攻击
{
公共列表成本{get;set;}
公共字符串名称{get;set;}
公共字符串文本{get;set;}
公共字符串损坏{get;set;}
public int convertedEnergyCost{get;set;}
}
公共阶级弱点
{
公共字符串类型{get;set;}
公共字符串值{get;set;}
}
公众阶级抵抗
{
公共字符串类型{get;set;}
公共字符串值{get;set;}
}
公共阶级能力
{
公共字符串名称{get;set;}
公共字符串文本{get;set;}
公共字符串类型{get;set;}
}
公务舱卡
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
public int nationalPokedexNumber{get;set;}
公共字符串imageUrl{get;set;}
公共字符串imageUrlHiRes{get;set;}
公共字符串子类型{get;set;}
公共字符串超类型{get;set;}
公共字符串hp{get;set;}
公共列表{get;set;}
公共字符串编号{get;set;}
公共字符串艺术家{get;set;}
公共字符串稀有性{get;set;}
公共字符串系列{get;set;}
公共字符串集{get;set;}
公共字符串setCode{get;set;}
公共列表类型{get;set;}
public List当它失败时,是否会给您一个异常或错误消息?@Jason不,它不会,它只是让我的jsonObject为空。我建议您仔细比较一个工作结果和一个非工作结果,并确定它们之间的区别。我会将json反序列化到一个空对象的内容记录下来,以了解我的问题。我使用了int作为number.While应该是一个字符串。这个名字让我很惊讶。从模型中跳过一些不必要的属性可以吗?它能反序列化json吗?你可以选择你想要反序列化的内容。通常JsonIgnore属性是一个很好的解决方案
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);