- Home /
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.
Answer by jnc1 · Feb 24, 2014 at 02:38 PM
@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);
}
}
Thanks you again and have a nice day
First of all i'm glad you found a way on your own. Actually I haven't written the parser as a general purpose parser in the first place, so i'm sorry this little helper was missing ;)
Of course some day ago i was in need of the keys as well and added a "$$anonymous$$eys" property:
// C#
// in JSONNode
public virtual IEnumerable<string> $$anonymous$$eys { get { yield break; } }
// C#
// in JSONClass
public override IEnumerable<string> $$anonymous$$eys
{
get
{
foreach(var key in m_Dict.$$anonymous$$eys)
yield return key;
}
}
With those you can simply do
// C#
foreach( var key in someJsonObject.$$anonymous$$eys )
{
Debug.Log(key);
}
// UnityScript
for( var key in someJsonObject.$$anonymous$$eys )
{
Debug.Log(key);
}
Or, since it's a IEnumerable, you can convert it to a List or array with Linq. Don't forget to import / using "System.Linq" at the top:
// C#
List<string> keyList = someJsonObject.$$anonymous$$eys.ToList();
string[] keyArray = someJsonObject.$$anonymous$$eys.ToArray();
// UnityScript
var keyList = someJsonObject.$$anonymous$$eys.ToList();
var keyArray = someJsonObject.$$anonymous$$eys.ToArray();
Someday i will update my parser on the wiki, but i don't have time at the moment ;)
I am the one who's glad to receive a comment from the SimpleJSON master :) If you already think of this that great for the future.
@sdgd : I prefer not being "selfish" and let the others approve the answer if that helped them.
@sdgd : I prefer not being "selfish" and let the others approve the answer if that helped them. --@jnc1 Feb 25 at 01:34 P$$anonymous$$
Others don't accept the answer. The one that asked the question marks the accepted answer, and in this case that's you.
I also just removed "[SOLVED]" from your topic title. Simply marking an accepted answer is sufficient.
Answer by wfq_Unity_count · May 19, 2016 at 02:18 PM
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); }
This is actually the correct answer. The only thing missing is the types used in $$anonymous$$eyValuePair.
It should be something like this:
foreach( $$anonymous$$eyValuePair<string, JSONNode> kvp in object_jNode.AsObject )
{
Debug.Log(kvp.$$anonymous$$ey + ", " + kvp.Value);
}
Right, though you can simply use "var" in this case.
foreach(var kvp in someNode)
{
Debug.Log(kvp.$$anonymous$$ey + ", " + kvp.Value);
}
SimpleJSON also comes with an implicit conversion of $$anonymous$$eyValuePair<string, JSONNode>
to JSONNode
. So if you iterate through an JSONArray or if you're only interested in the values of an object you can do
foreach(JSONNode n in someNode)
{
}
This assumes you used the latest version on github. The latest version uses a struct enumerator which doesn't allocate memory. Of course this enumerator has to work for arrays as well as objects. That's why it always works with $$anonymous$$eyValuePairs and why the implicit conversion exists. Though the JSONNode also has a $$anonymous$$eys and a Values enumerable (also a struct enumerator of course) to iterate through only the keys or values.
The struct enumerator doesn't work with Linq since it doesn't implement the IEnumerable interface (if it would a bug in mono would box the struct enumerator and cause memory allocations). Thats why you can use someNode.Linq
if you want to do some Linq magic.
Answer by Kiloblargh · Feb 23, 2014 at 09:21 PM
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();
Your answer
Follow this Question
Related Questions
Optimising dictionary load (in Javascript) 2 Answers
dictionary, random element (js) 1 Answer
Javascript TextAsset to Generic Dictionary 1 Answer
scroll through buttons with arrow keys 1 Answer
Generic dictionary emptying itself? 0 Answers