We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I would like to know if in Processing an hashmap con include interger and string value as python where the dictionary can include , numbers and string value.
To be more specific , I would like include into the hashmap something like this: ("key1" : 10, "key2" :"home", "key3" : 15,3)
In all documentation I see that is mentioned that key must be string and values integer or float , but I didn't find something that say that values can be mixed inside the same hashmap.
Thanks.
Answers
You can have a HashMap that uses Object as the key and/or value type. But you'll have to cast the value you get from it.
0
, you know it was a String and not a number type.Is there any smart way to fill a StringDict using a string received via serial ? Use the .set(string,string) became quite complex in case the received data is like this " {"key1":"val1","key2":"val2","key3":"val3","key4":"val4"........."keyx":"valx"}
Is it possible use something similat to JSON dump/load also with StringDict ? Thanks.
I don't believe StringDict 's set() is capable to parse some arbitrary complex String:
https://Processing.org/reference/IntDict_set_.html
Before anything, issue trim() over the received String:
https://Processing.org/reference/trim_.html
You're gonna need to replace() every
{
,}
&"
chars w/ an empty""
:http://docs.Oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-
Then split() it by
,
. And again split() each 1 by:
before feeding the resultant array pair to set():https://processing.org/reference/split_.html
Since the arrived String is 1 JSON object already, why not directly get it via loadJSONObject()?: :P
https://Processing.org/reference/loadJSONObject_.html
Yes is what I'm locking to do, but probably I've some trouible with the format of data that I send between the two applications , the first written in python and the one on host written with Processing2.
I've used the example from the link and substituted the source of data received with the string inBuffer, but I got the error **unexpected token:: **. I've not clear how the inBuffer must be declared.....
`
`
Please read about how to post properly formatted code here:
https://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
In Java a String sequence is wrapped by
"
on both of its extremities.Examples:
"header"
,", "
, etc.However, if the content got
"
inside it, we use\
, just like the other escape chars:\n
,\t
, etc:"{ \"header\":\"$$$\", \"setting_a\":\"256\" }"
Hi , I'm back again with my problem using the json in Processing2.
After the above reply I've modified the code but when it run I get an error on the line where i load the string into json object: inBuffer does not exist or could not be read
I don't know why inBuffer seems not exist while the println show exactly the right string. :(
There are some couple of errors above: :-O
loadJSONObject("inBuffer")
you're passing a literal String, while clearly the intention was to pass a variable called inBuffer instead:loadJSONObject(inBuffer)
.https://Processing.org/reference/loadJSONObject_.html
https://Processing.org/reference/parseJSONObject_.html
https://processing.org/reference/println_.html
println(st1 + "," + str(st2));
:println(st1 + ',' + st2);
+
operator is overloaded as a concatenator if at least 1 of its operands is a String:https://Processing.org/reference/addition.html
Very clear as ususal !
Thanks you .
Hi, in which way can I convert a jsonobject to string that can be sent via serial port?
If I try :
myPort.write(json)
I got an error as : the method write(byte[]) in the type Serial is not applicable for the argument (JSONObject).
byte[]
,int
or String.myPort.write(json.toString());