Hashmap with integer , float and string values.

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.

  • Answer ✓
    • You can use StringDict and store all values as String.
    • When reading, you may try to int() or float() them.
    • If the result is 0, you know it was a String and not a number type.
    • Another very good option to mingle types is Table
    1. https://Processing.org/reference/StringDict.html
    2. https://Processing.org/reference/Table.html
    3. https://Processing.org/reference/intconvert_.html
    4. https://Processing.org/reference/floatconvert_.html
  • edited December 2015

    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.

  • edited December 2015

    {"key1":"val1","key2":"val2","key3":"val3","key4":"val4"........."keyx":"valx"}

    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

  • edited December 2015

    Since the arrived String is 1 JSON object already, why not directly get it via loadJSONObject()?: :P
    https://Processing.org/reference/loadJSONObject_.html

  • edited December 2015

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

    `

    JSONObject json;
    
    String inBuffer = {"header":"$$$",  "setting_a":"256",  "hrs_b":"23",  "mins_b":"59",  "hre_b":"23",  "mine_b":"59",  "in_sonda_a":"1111"};
    
    void setup() 
    {
      json = loadJSONObject("inBuffer");
    
      String st1 = json.getString("header");
      String st2 = json.getInt("setting_a");
      String st3 = json.getInt("min_rtc");
    
      println(st1 + ", " + st2 + ", " + st3);
    }
    

    `

  • Answer ✓

    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

        void setup() 
        {
          String inBuffer ="{\"header\":\"$$$\",\"setting_a\":\"256\"}";
          int lnx=inBuffer.length();
          println(str(lnx));
          println(inBuffer);
          JSONObject json = loadJSONObject("inBuffer");
          String st1 = json.getString("header");
          int st2 = json.getInt("setting_a");
          println(st1 + "," + str(st2) );
    
        }
    

    I don't know why inBuffer seems not exist while the println show exactly the right string. :(

  • edited January 2016 Answer ✓

    There are some couple of errors above: :-O

    1. @ loadJSONObject("inBuffer") you're passing a literal String, while clearly the intention was to pass a variable called inBuffer instead: loadJSONObject(inBuffer).
    2. loadJSONObject()'s String or File parameter is supposed to represent a filename path or URL:
      https://Processing.org/reference/loadJSONObject_.html
    3. However, variable inBuffer represents a JSON object as a String.
    4. For such we'd use parseJSONObject() in order to get the actual JSONObject outta it:
      https://Processing.org/reference/parseJSONObject_.html
    5. We don't need to convert anything to String in order to use it in println() at all:
      https://processing.org/reference/println_.html
    6. For example, no need for str() @ println(st1 + "," + str(st2));: println(st1 + ',' + st2);
    7. In Java the + operator is overloaded as a concatenator if at least 1 of its operands is a String:
      https://Processing.org/reference/addition.html

    // forum.Processing.org/two/discussion/14046/
    // hashmap-with-integer-float-and-string-values
    
    // 2016-Jan-23
    
    String inBuffer = "{ \"header\": \"$$$\", \"setting_a\": \"256\" }";
    println(inBuffer, ENTER);
    
    JSONObject json = parseJSONObject(inBuffer);
    println(json, ENTER);
    
    String header = json.getString("header");
    int setting_a = json.getInt("setting_a");
    println(header + ", " + setting_a);
    
    exit();
    
  • 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).

  • edited February 2016 Answer ✓
Sign In or Register to comment.