2017-05-16 62 views
0

我试图反序列化一些使用JsonConver.DeserializeObject的json。然而它不适用于我使用的api中的某些json。这里是一个不起作用的链接:https://api.pokemontcg.io/v1/cards?setCode=smpJsonConvert.DeserializeObject有时不工作

这里是一个可以工作的链接。 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; } 
    } 
} 

如果妳可以帮助我我会觉得很神奇。

+0

当它失败了,它是否会给你一个异常或错误信息? – Jason

+0

@Jason不,它只是让我的jsonObject空着。 –

+0

我建议你仔细比较一个工作结果和一个不工作的结果,找出差异。 – Jason

回答

0

我测试了你的json。

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

    } 
} 

而且这是我所说的反序列化

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

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

this is the result

+0

我看到我的问题。我使用了一个int数字。虽然它应该是一个字符串。这个名字使我失望了。 –