c# - 调用 JSON Web API 并获取数据

c# - Call JSON Web API and get data

我正在尝试从 Json Web API 访问各个标签。如果您查看我的调试消息,您可以看到 "jsonMessage" 中每个标签都显示了正确的数据,但是当我 return "result" 每个标签都设置为 null:

那么如何获取从 jsonMessage 到 return 的所有标签,这样我就可以输入 texblock.text = card.name; 等等

public async static Task<Card> GetCard()
{
    string url = String.Format("https://api.magicthegathering.io/v1/cards/386616");

    HttpClient client = new HttpClient();
    //client.BaseAddress = new Uri(url);

    var response = await client.GetAsync(url);

    var jsonMessage = await response.Content.ReadAsStringAsync();
    var serializer = new DataContractJsonSerializer(typeof(Card));
    var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonMessage));

    var result = (Card)serializer.ReadObject(ms);
    return result;
}

这是卡片 class:

public class Card
{
    public string name { get; set; }
    public string manaCost { get; set; }
    public int cmc { get; set; }
    public List<string> colors { get; set; }
    public string type { get; set; }
    public List<string> types { get; set; }
    public List<string> subtypes { get; set; }
    public string rarity { get; set; }
    public string set { get; set; }
    public string text { get; set; }
    public string artist { get; set; }
    public string number { get; set; }
    public string power { get; set; }
    public string toughness { get; set; }
    public string layout { get; set; }
    public int multiverseid { get; set; }
    public string imageUrl { get; set; }
    public List<Ruling> rulings { get; set; }
    public List<ForeignName> foreignNames { get; set; }
    public List<string> printings { get; set; }
    public string originalText { get; set; }
    public string originalType { get; set; }
    public List<Legality> legalities { get; set; }
    public string id { get; set; }
}

您好,查看您的调用结果,您必须将其包装到一个结果对象中。

public class CardResult
{
    public Card card { get; set; }
}

要正确反序列化,您的 Card 对象必须位于具有 属性 类型 Card 的对象中。我使用 JsonUtils 重新创建了 类 并不是说​​它是必需的,但我更喜欢拥有这些属性,这样我就可以自由地重命名属性而不必担心破坏 serialization/deserialization。我还更新了您的代码片段以使其正常工作:

[DataContract]
public class Ruling
{

    [DataMember(Name = "date")]
    public string Date { get; set; }

    [DataMember(Name = "text")]
    public string Text { get; set; }
}

[DataContract]
public class ForeignName
{

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "language")]
    public string Language { get; set; }

    [DataMember(Name = "multiverseid")]
    public int Multiverseid { get; set; }
}

[DataContract]
public class Legality
{

    [DataMember(Name = "format")]
    public string Format { get; set; }

    [DataMember(Name = "legality")]
    public string LLegality { get; set; }
}

[DataContract]
public class Card
{

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "manaCost")]
    public string ManaCost { get; set; }

    [DataMember(Name = "cmc")]
    public int Cmc { get; set; }

    [DataMember(Name = "colors")]
    public IList<string> Colors { get; set; }

    [DataMember(Name = "type")]
    public string Type { get; set; }

    [DataMember(Name = "supertypes")]
    public IList<string> Supertypes { get; set; }

    [DataMember(Name = "types")]
    public IList<string> Types { get; set; }

    [DataMember(Name = "subtypes")]
    public IList<string> Subtypes { get; set; }

    [DataMember(Name = "rarity")]
    public string Rarity { get; set; }

    [DataMember(Name = "set")]
    public string Set { get; set; }

    [DataMember(Name = "text")]
    public string Text { get; set; }

    [DataMember(Name = "artist")]
    public string Artist { get; set; }

    [DataMember(Name = "number")]
    public string Number { get; set; }

    [DataMember(Name = "power")]
    public string Power { get; set; }

    [DataMember(Name = "toughness")]
    public string Toughness { get; set; }

    [DataMember(Name = "layout")]
    public string Layout { get; set; }

    [DataMember(Name = "multiverseid")]
    public int Multiverseid { get; set; }

    [DataMember(Name = "imageUrl")]
    public string ImageUrl { get; set; }

    [DataMember(Name = "watermark")]
    public string Watermark { get; set; }

    [DataMember(Name = "rulings")]
    public IList<Ruling> Rulings { get; set; }

    [DataMember(Name = "foreignNames")]
    public IList<ForeignName> ForeignNames { get; set; }

    [DataMember(Name = "printings")]
    public IList<string> Printings { get; set; }

    [DataMember(Name = "originalText")]
    public string OriginalText { get; set; }

    [DataMember(Name = "originalType")]
    public string OriginalType { get; set; }

    [DataMember(Name = "legalities")]
    public IList<Legality> Legalities { get; set; }

    [DataMember(Name = "id")]
    public string Id { get; set; }
}

[DataContract]
public class ApiResult
{

    [DataMember(Name = "card")]
    public Card Card { get; set; }
}

这是反序列化代码:

var response = await  client.GetAsync(url);

var jsonMessage = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(ApiResult));
ApiResult result;
// Disposing of the MemoryStream after the serialization to save 
// some memory space
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonMessage)))
{
    result = (ApiResult) serializer.ReadObject(ms);
}

现在您可以使用:

texblock.text = result.Card.name;

附带说明一下,您应该考虑使用 Json.Net 而不是默认的 .Net 反序列化器,因为它速度更快并且功能更多。