-1
  • Do not say there is built in Unity Json or to download the Lindq for C# I am using SimpleJSON

I am trying to read the json file and add the elements to a list so I can call them easier. I cannot figure out how to add all of "Heads" values to list.

So I could possibly call

listname[index].key
listname[key].value

to return somthing where I can get example.("Small_Cube_Head":{"Colors":{"Default"})

I basically want to be able to call the "Small_Cube_Head" and all color values

JSON FILE

{
"Heads":{
        "Large_Cube_Head":["Default","e97451"],
        "Medium_Cube_Head":["Default","ffffff","76d7ea","e97451","5fa777","f4c430","d0ff14","0047ab","e32636","fc419a","720b98","ff8b00","000000","848482"],
        "Small_Cube_Head":["Default"]
        }
    }

}

code

    /**
     * json: Returns the whole file as a String
     **/
    private void LoadJson()
    {
    using (StreamReader r = new StreamReader("Assets/JSON/PlayerPartsList.json"))
    {
        json = r.ReadToEnd();
        //Debug.Log(json);
        JSONNode node = JSON.Parse(json);

        Debug.Log(node["Heads"].Count); //returns 3 
        for (int i = 0; i < node["Heads"].Count; i++) {
            //headParts.Add(node["Heads"].);
            //Debug.Log(node["Heads"][i].Value.ToString());
            //Debug.Log(node["Heads"]["Small_Cube_Head"].Value);

        }
    }
    }
user3277468
  • 141
  • 2
  • 11
  • It's unclear what you're trying to do. –  May 17 '19 at 17:33
  • I want to put the key and values inside of "Heads" to a list, so with the JSON file there would be 3 (key,value) in a list. @Amy – user3277468 May 17 '19 at 17:34
  • Your JSON file does not contain valid JSON – Ruzihm May 17 '19 at 17:35
  • `{"Default","e97451"}`, `{"Default","ffffff","76d7ea","e97451","5fa777","f4c430","d0ff14","0047ab","e32636","fc419a","720b98","ff8b00","000000","848482"}`, and `{"Default"}` are not valid JSON values – Ruzihm May 17 '19 at 17:37
  • 1
    @Ruzihm Ah, you're right. I was mentally changing it to an array. Nice catch. –  May 17 '19 at 17:38
  • I'm voting to close this as an off-topic question (typo) because the commented out code probably would work as intended if the json file were valid. – Ruzihm May 17 '19 at 17:44
  • I fixed the file the commented out code still doesnt work @Ruzihm – user3277468 May 17 '19 at 17:46
  • 1
    What happens when you try it? Can you be more specific than "doesn't work"? Are you getting an exception? What actually happens? –  May 17 '19 at 17:47
  • "It doesn't work" sounds like your code doesn't compile. Please be more descriptive. – Ruzihm May 17 '19 at 17:47
  • It just shows that everything is a Object @Ruzihm – user3277468 May 17 '19 at 17:49

1 Answers1

1

Use Keys like an enumerator to get each head name, and then you can loop through the count of that headName as the index

KeyEnumerator headNameEnum = node["Heads"].Keys;

while (headNameEnum.MoveNext())
{
    String headName = headNameEnum.Current().Value;

    Debug.Log("headName: " + headName); 
    for (int i=0; i < node["Heads"][headName].Count; i++) {
        String valueName = node["Heads"][headName][i].Value;
        Debug.Log("valueName: " + valueName);
    }

}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48