0

im trying to display a json list of items gathered from a web api, in unity, just to debug and then later in a text field for users. however the list always displays as empty and i cannot figure out why.

where i try to gather and display the data:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class SearchThroughCards : MonoBehaviour {
    public int size;
    // Use this for initialization
    void Start () {
        StartCoroutine(GetCards());

    }

    // Update is called once per frame
    IEnumerator GetCards()
    {
        UnityWebRequest www = UnityWebRequest.Get("https://www.ygohub.com/api/all_cards");
        yield return www.Send();

        if (www.isNetworkError)
        {
            Debug.Log(www.error);
        }
        else
        {
            // Show results as text
            Debug.Log(www.downloadHandler.text);
            RootCardList cardList = JsonUtility.FromJson<RootCardList>(www.downloadHandler.text);
            size = cardList.card.Count;
            Debug.Log(cardList.card);
            foreach (string str in cardList.card)
            {
                Debug.Log(str);
            }
        }


    }
}

"size" always gets debugged as 0 and the foreach loop actually never occurs my class for the data:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class RootCardList
{
    public string status;
    public List<string> card;
} 

a snippet of the json data im trying to convert:

{"status":"success","cards":["\"A\" Cell Breeding Device","\"A\" Cell Incubator","\"A\" Cell Recombination Device","\"A\" Cell Scatter Burst","1st Movement Solo","3-Hump Lacooda","30,000-Year White Turtle","4-Starred Ladybug of Doom","7","7 Colored Fish",
evilsponge
  • 81
  • 7
  • 1
    You have to post the complete Json from the log. If you can't do that, use the link I posted in your other question to generate the class. This is still a duplicate. – Programmer Jul 15 '17 at 15:53
  • i used the link to generate the class, the complete json is too long and gets truncated to post here "Blackwing - Gale the Whirlwind","Blackwing - – evilsponge Jul 15 '17 at 15:57
  • the entirity of the json data can be seen: [link](https://www.ygohub.com/api/all_cards) – evilsponge Jul 15 '17 at 15:58
  • Your code is not logging out `size` or `cardList.card.Count` so I don't see where you got that `0` from.... – Programmer Jul 15 '17 at 16:00
  • that was an error on my part i was in the process of testing something, the 0 came when i debugged out the size int – evilsponge Jul 15 '17 at 16:04
  • 1
    You claimed that you copied it from the link I made but you did not. You wrote it is by hand. The variable is supposed to be `cards` **not** `card`. That's why you are getting `0`. – Programmer Jul 15 '17 at 16:43
  • wow... i genuinely copied it but modified that for a unknown reason, thanks alot. – evilsponge Jul 15 '17 at 16:45

0 Answers0