Results 1 to 6 of 6

Thread: hashmap to json

  1. #1
    Join Date
    Dec 2009
    Posts
    13
    Rep Power
    0

    Default hashmap to json

    hi am trying to convert hashmap to json array using JSONObject jsonObject = JSONObject.fromObject( map ); method but not able to run it,tomcat throws an error i have fallowing jar files jsonlib-2.3.jar,commonbeansutil.jar,commons-collection.jar,commons-logging.jar,ezmorph.jar please let me know if any additiona;l jars are required

  2. #2
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi prakashkadakol,
    Can you copy and paste the full stack trace from tomcat? That would help troubleshoot this issue.
    Thanks,
    yanivby

  3. #3
    Join Date
    Dec 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by yanivby View Post
    Hi prakashkadakol,
    Can you copy and paste the full stack trace from tomcat? That would help troubleshoot this issue.
    Thanks,
    yanivby
    net.sf.json.JSONException: java.lang.ClassCastException: JSON keys must be strings.
    net.sf.json.JSONObject._fromMap(JSONObject.java:13 57)
    net.sf.json.JSONObject.fromObject(JSONObject.java: 159)
    net.sf.json.JSONObject.fromObject(JSONObject.java: 130)
    Combo.Country.doGet(Country.java:57)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)


    Mine Code is
    HashMap mp = new HashMap();
    mp = db.fetchcountry();
    JSONObject jsonObject = JSONObject.fromObject( mp );
    pw.println(jsonObject);
    here db.fetchcountry() will return hashmap with key->value pair

  4. #4
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    JSONObject is complaining that the keys in the map you supplied are not of type String. It requires the keys to be strings.
    Of what class are the keys in mp? If you're not sure, add after the third line:

    Class clazz = mp.keySet().iterator().next().getClass(); // assert !mp.isEmpty()
    System.out.println(clazz); // hopefully you have access to stdout from tomcat. otherwise, print to the response, or write a standalone test class.

    Next, check if you can convert the keys into strings. Perhaps that class toString() is enough, but you'll have to check the implementation and/or test it.

    Hope this helps!
    yanivby

  5. #5
    Join Date
    Dec 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by yanivby View Post
    JSONObject is complaining that the keys in the map you supplied are not of type String. It requires the keys to be strings.
    Of what class are the keys in mp? If you're not sure, add after the third line:

    Class clazz = mp.keySet().iterator().next().getClass(); // assert !mp.isEmpty()
    System.out.println(clazz); // hopefully you have access to stdout from tomcat. otherwise, print to the response, or write a standalone test class.

    Next, check if you can convert the keys into strings. Perhaps that class toString() is enough, but you'll have to check the implementation and/or test it.

    Hope this helps!
    yanivby
    thanx yanivby i have done mistake,i corrected now earlier used to create hashmap like hashmap = new hashmap now i changed to hashmap<String ,String> mp = new HashMap

  6. #6
    gburdett is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default

    Here is some sample code. The code iterates over an enumeration of key / pair values in a properties bundle, creates a HashMap, and then converts that HashMap into a JSON string.

    import org.json.simple.*; //Requires json_simple-1.1.jar file
    ...
    Enumeration<String> keys = loader.getKeys();
    Map<String,String> map = new HashMap<String,String>();
    while (keys.hasMoreElements()) {
    String key = keys.nextElement();
    map.put(key, loader.getProperty(key));
    }
    String jsonText = JSONValue.toJSONString(map);
    ....

    Greg Burdett

Similar Threads

  1. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  2. JSON from javascript to java servlet
    By boss-tech in forum Java Servlet
    Replies: 1
    Last Post: 09-18-2009, 09:27 AM
  3. HashMap
    By koolhoney in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 09:08 PM
  4. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •