Android:How to convert the ExitGames::Common::Hashtable to java HashMap
Hello,
I am trying to get the properties of a user (or a room), and I need to create a HashMap from the HashTable object. Could someone please assist me on this task?
This is what I have so far:
Thank you
I am trying to get the properties of a user (or a room), and I need to create a HashMap from the HashTable object. Could someone please assist me on this task?
This is what I have so far:
jclass clazz = mEnv->FindClass("java/util/HashMap"); if(clazz != NULL) { jmethodID init = mEnv->GetMethodID(clazz, "<init>", "()V"); jobject hashMap = mEnv->NewObject(clazz, init); jmethodID put = mEnv->GetMethodID(clazz, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); char *str = "p"; jstring jstrBuf = mEnv->NewStringUTF(str); jclass cls = mEnv->FindClass("java/lang/Integer"); jmethodID midInit = mEnv->GetMethodID( cls, "<init>", "(I)V"); jobject newObj = mEnv->NewObject(cls, midInit, player.getNumber()); mEnv->CallObjectMethod(hashMap, put, jstrBuf, newObj); jclass stringClazz = mEnv->FindClass("java/lang/String"); jmethodID id = mEnv->GetMethodID(stringClazz, "<init>", "([BLjava/lang/String;)V"); const ExitGames::Common::Hashtable& properties = player.getCustomProperties(); const ExitGames::Common::JVector<ExitGames::Common::Object>& keys = properties.getKeys(); int i; int r = keys.getSize(); for(i) // normal forloop, simplified because the editor modifies it { ExitGames::Common::Object key = keys[i]; jstring keyBuf = mEnv->NewStringUTF(key.toString().UTF8Representation().cstr()); const ExitGames::Common::Object *value = properties.getValue(key); jstring valueBuf = mEnv->NewStringUTF(value->toString().UTF8Representation()); mEnv->CallObjectMethod(hashMap, put, keyBuf, valueBuf); } }This does populate the HashMap, but all keys are doubled quoted, e.g. ""Demo"". Why do I have this ?
Thank you
0
Comments
-
The good thing is that the function toString() returns a representation of the HashTable which is almost a json object. With only one substitution, I can parse it as a JSONObject.
Still, I would like to know what is the error in my code.
Thank you0 -
Hi @Vasilis.
I don't know what the Java Hashmap class does internally, but it looks like one pair of quotes must come from there.
The other pair of quoted is added by thetoString()
calls.
PhotonstoString()
API returns a string representation of the object on which it is called. If that object itself is an instance of a string class, then thetoString()
call adds quotes around the returned string representation of it. If you calltoString()
on a container class like aDictionary
,Hashtable
orJVector
, then thetoString()
implementation of that container class will iterate over all its elements and calltoString()
on them. CallingtoString()
on an instance of classObject
or of a subclass of it will result in a call oftoString()
on its payload.
WhentoString()
is called on aJString
instance (which will happen if you call it on anObject
instance that holds aJString
), then it will return the payload of that strring wrapped into quotation marks.
This is the case because it makes it easier to spot strings in the string representations of complex containers structures (which are in turn the structures for whichtoString()
is the most useful).
The primary intended usage scenarios fortoString()
are logging and debugging: If the payload of a complex container does not seem to look like you expected, then it often helps to print a string representation of the containers inner structure in a human-readable representation.
When you want to retrieve the payload of anObject
instance, then the proper way to do it is operate through the matchingKeyObject
orValueObject
template for the payload type. So in the case of a key object that holds a string you would access the payload of that object throughKeyObject< JString>::getDataCopy()
. That way you get the original payload string and not a string representation of theObject
like with atoString()
call
HenceKeyObject< JString>::getDataCopy()
returns the string without additional quotes whileObject::toString()
returns it with extra quotes.0 -
0
-
It's worth noting that the order of the elements in the HashMap may not be the same as the order of elements in the original Hashtable, as the order is not guaranteed in either data structure. Additionally, if there are any null keys or values in the Hashtable, they will throw a NullPointerException in the HashMap, as HashMap does not allow null keys or values.
0