I'm trying to access the individual tags from the Json Web API. if you look at my Debug message you can see that every tag is shown with correct data in "jsonMessage", but when i return "result" every tag is set to null:
So how to I get all the tags from jsonMessage
to return so I can just type for example texblock.text = card.name;
and so forth
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;
}
Here's the Cards 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; }
}
To deserialize correctly your Card
object must be in an object that has a property of type Card
. I recreated the classes using JsonUtils Not that it was necessary but I prefer having the attributes so that I can freely rename the properties without worrying about breaking the serialization/deserialization. I've also updated your code snippet to work correctly:
[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; }
}
And here's the deserialization code:
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);
}
Now you can use:
texblock.text = result.Card.name;
On a side note your should consider using Json.Net instead of the default .Net deserializer because it's faster and has way more features.