Home > Net >  How to deserialize and use property of JSON Nested Arrays
How to deserialize and use property of JSON Nested Arrays

Time:07-17

I'm making a C# project on Unity based on the card game Yu-Gi-Oh . I'm using this API: https://db.ygoprodeck.com/api-guide/

The API give access to all the cards of the game with their info (name, effect, ATK value) in a JSON. But before using this JSON, I'm working with a JSON in a txt file which is written with the same model as the API's , this way I don't make unnecessary requests to their servers until my project is done, and it's easier to test because the API contains thousands of cards, the JSON I'm currently experimenting with only contains 1 card.

There it is:

{
   "data":[
      {
         "id":6983839,
         "name":"Tornado Dragon",
         "type":"XYZ Monster",
         "desc":"2 Level 4 monsters\nOnce per turn (Quick Effect): You can detach 1 material from this card, then target 1 Spell/Trap on the field; destroy it.",
         "atk":2100,
         "def":2000,
         "level":4,
         "race":"Wyrm",
         "attribute":"WIND",
         "card_images":[
            {
               "id":6983839,
               "image_url":"https://storage.googleapis.com/ygoprodeck.com/pics/6983839.jpg",
               "image_url_small":"https://storage.googleapis.com/ygoprodeck.com/pics_small/6983839.jpg"
            }
         ]
      }
   ]
}

I would like to get the name , the desc (description) , and the image_url of the card. I managed to display the name and the description , however I don't know how I could get access to the image_url located in the second array of the JSON (card_image array) .

There are my classes, and how I got to display the card name and desc:

public class Root
{
  public Card[] data { get; set; }

}

public class Card
{
  public int id { get; set; }
  public string name { get; set; }
  public string desc{ get; set; }
  public CardImage[] card_images { get; set; }
}

public class CardImage
{
  public int id { get; set; }
  public string image_url { get; set; }
  public string image_url_small{ get; set; }
}




void Start()
{
    string jsonString = File.ReadAllText("myTxtJson.txt");
    var cards = JsonHelper.FromJson<Root>(jsonString);
    
    foreach(Card card in cards.data)
    {
      Debug.Log(card.name); // Display the name of the card
      Debug.Log(card.desc);  // Display the effect of the card
    }

}

The point would be (if possible) to have a "simple" access to the 3 properties I need which are card.name, card.desc and card.image_url . Thanks if you can help me, I'm new to OOP so sorry if there are big mistakes, I'll take any advices on the existing code as well.

CodePudding user response:

a professional would be using Linq

using System.Linq;

    List<CardInfo> cardInfoList = cards.data.Select(d => new CardInfo
    {
        id = d.id,
        name = d.name,
        image_url = d.card_images.Select(d => d.image_url).ToList()
    }).ToList();

foreach(CardInfo card in cardInfoList )
    {
      Debug.Log(card.name); // Display the name of the card
      Debug.Log( string.Join(",", card.image_url);  // Display the urls of the card
    }

// or you can find a card by name

    CardInfo tornadoDragonCardInfo = cardInfoList
                                     .Where(d => d.name == "Tornado Dragon")
                                     .FirstOrDefault();

    int tornadoDragonId = tornadoDragonCardInfo.id;

    string tornadoDragonFirstImgUrl = tornadoDragonCardInfo.image_url[0];

public class CardInfo
{
    public int id { get; set; }
    public string name { get; set; }
    public List<string> image_url { get; set; }
}

but if you need something very simple, you don't need any extra class

    int tornadoDragonId = cards.data[0].id;
    string tornadoDragonName = cards.data[0].name;
    string tornadoDragonImgUrl = cards.data[0].card_images[0].image_url;

but it will be working for only your particular json

  • Related