How to get the keys of a SimpleJSON dictionary ?

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.

Thanks you for your help.

p.s : I’m writting in JavaScript.

@Kiloblargh : I couldn’t wait to test it, and unfortunately I must say that it didn’t worked :frowning:

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);
	}
}

Thanks you again and have a nice day

Hi,i get it ,you can do this ,

jar is the JSONNode of JSONArray

foreach (KeyValuePair kvp in jaR.AsObject) { Debug.Log(kvp.Key + "---" + kvp.Value); }

Dictionary.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();