Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by jnc1 · Feb 23, 2014 at 08:42 PM · javascriptdictionarykeys

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.

Comment
Comments Locked · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ReVo_ · Feb 23, 2014 at 08:47 PM 0
Share

Have you tried objectReturned[$$anonymous$$EY] ?

avatar image jnc1 · Feb 24, 2014 at 12:10 PM 0
Share

I'm trying to get the keys, not the values, but thanks ^^

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Feb 24, 2014 at 03:51 PM 1
Share

if it works you should accept answer to help others.

avatar image Bunny83 · Feb 25, 2014 at 12:49 PM 6
Share

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

avatar image jnc1 · Feb 25, 2014 at 01:34 PM 0
Share

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.

avatar image hello1111 · Jun 02, 2014 at 10:13 PM 0
Share

thank you. this is great.

avatar image Lo0NuhtiK · Jun 02, 2014 at 10:22 PM 0
Share

@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.

Show more comments
avatar image
3

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanton18 · Mar 25, 2019 at 12:54 PM 1
Share

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

avatar image Bunny83 tanton18 · Mar 25, 2019 at 01:04 PM 0
Share

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.

avatar image
2

Answer by Kiloblargh · Feb 23, 2014 at 09:21 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jnc1 · Feb 24, 2014 at 12:09 PM 0
Share

That seems interesting, I'll try this out tonight :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges