- Home /
How do you interate through a JSON object with a foreach loop to add each element to a list?
I'm trying to add elements of a JSON object to a list so I can loop through the list and show the results onscreen with a prefab. Only When I debug out the List.Count it's zero. Please show me where I'm messing up. Thanks!
public List<KeyValuePair<string, JSONValue>> NameList;
void Start ()
{
NameList=new List<KeyValuePair<string, JSONValue>>();
}
public void ShowTheName()
{
if(File.Exists(Application.persistentDataPath + "/TheName.json"))
{
Debug.Log("File Exists!");
string Namedata; // variable to be used when loading in the json file.
JSONObject NameObject =new JSONObject(); // name of the variable that will parse the string back into a json object
Namedata=File.ReadAllText(Application.persistentDataPath + "/Name.json");
Debug.Log("Loaded JSONString=" + Namedata);
NameObject= JSONObject.Parse(Namedata); //parse our file
Debug.Log(NameObject);
foreach (KeyValuePair<string, JSONValue> keyValuePair in NameObject)
{
NameObject.Add(keyValuePair);
Debug.Log(NameObject);
}
Comment