5 Replies Latest reply: Mar 9, 2011 5:39 AM by EJP RSS

    HashMap from String

    795605
      I have such a String from which I would like to create a HashMap:
      {
           uid=3, 
           body=My name is Graham Masinde, 
           data=a:1:{s:13:"form_build_id";s:37:"form-ed31658feb6b05b507fc07bfe9282d1d";}, 
           type=story, 
           last_comment_name=, 
           changed=1299584224, 
           title=Name, 
           created=1299157495, 
           name=arthur, 
           revision_uid=1, 
           taxonomy=[Ljava.lang.Object;@16e0054, 
           tnid=0, 
           vid=16, 
           comment_count=0, 
           status=1, 
           nid=16, 
           moderate=0, 
           format=1, 
           log=, 
           picture=, 
           sticky=0, 
           promote=1, 
           teaser=My name is Graham Masinde, 
           last_comment_timestamp=1299157495, 
           revision_timestamp=1299584224, 
           translate=0, 
           language=, 
           comment=2
      }
      I would have crawled the String using StringTokenizer, but am worried about the key value "body" and/or "teaser having the StringTokenizer delimiters that are intuitive for my String(= or ,).

      A point to note is that the String is one line and am the one who has manually added the carriage returns in order to improve readability.

      My question therefore is, is there a method to convert such a String into a HashMap please?
        • 1. Re: HashMap from String
          EJP
          You could have a lok at Properties.load(), after you've removed the trailing commas and the parentheses.
          • 2. Re: HashMap from String
            795605
            Properties.load seems to be a good idea but seems that you missed my point of note,
            A point to note is that the String is one line and am the one who has manually added the carriage returns in order to >improve readability.
            This is my code:
            String node = "uid=3, body=My name is Graham Masinde, data=a:1:{s:13:\"form_build_id\";s:37:\"form-ed31658feb6b05b507fc07bfe9282d1d\";}, type=story, last_comment_name=, changed=1299584224, title=Name, created=1299157495, name=arthur, revision_uid=1, taxonomy=[Ljava.lang.Object;@16e0054, tnid=0, vid=16, comment_count=0, status=1, nid=16, moderate=0, format=1, log=, picture=, sticky=0, promote=1, teaser=My name is Graham Masinde, last_comment_timestamp=1299157495, revision_timestamp=1299584224, translate=0, language=, comment=2";
            
                    Properties props = new Properties();
                    try
                    {
                        props.load(new ByteArrayInputStream(node.getBytes()));
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
            
                    System.out.println(props.getProperty("body"));
            That outputs:
            null
            • 3. Re: HashMap from String
              802316
              How about something like
              String text = "A=B=1, C = D, E";
              Map<String, String> map = new LinkedHashMap<String, String>();
              for(String keyValue : text.split(" *, *")) {
                 String[] pairs = keyValue.split(" *= *", 2);
                 map.put(pairs[0], pairs.length == 1 ? "" : pairs[1]);
              }
              • 4. Re: HashMap from String
                795605
                Thanks Peter. Works ok :-)
                • 5. Re: HashMap from String
                  EJP
                  Properties.load seems to be a good idea but seems that you missed my point of note
                  No, you missed the point of my note where I mentioned modifying the string first.