Hello, I am parsing a string into SimpleJSON to get a dictionary and then I need to get all the keys of this one, is there a way, a method to get the keys ? I couldn’t find anything in Google.
@Kiloblargh : I couldn’t wait to test it, and unfortunately I must say that it didn’t worked
The dictionary returned by SimpleJSON is a very complicated object without “Keys()” method.
However you gave me the strength to search again, and later an idea… Why not write a method in C# to get the keys (I’ve never written in C#).
I modified the SimpleJSON.cs that can be found here and added the following lines in the JSONClass (around lines 677) :
public ArrayList GetKeys() // The method is named "GetKeys()"
{
ArrayList arrayOfStrings = new ArrayList(); // declares new array
foreach (KeyValuePair<string, JSONNode> N in m_Dict) // for each key/values
arrayOfStrings.Add(N.Key); // I add only the keys
return arrayOfStrings; // And then I get them all :D
}
In javascript I get the keys :
import SimpleJSON; // Importing the SimpleJSON.cs file
var str : String = '{"0101": "TG", "0308": "C"}'; // The json string
function Start() {
var dict = JSONNode.Parse(str); // I parse the string into SimpleJSON to get the dictionary
var test = dict.GetKeys(); // I get my array of keys
for (var keys : String in test) { // and then display them through a for loop
Debug.Log(keys);
}
}
Only thing to watch out for- I think this gets you a type “Collection” which is almost as useless in Unity as a type “Object”.
So you have to do it like this:
var keyArray : TKey[] = yourDictionary.Keys().ToArray();