String to HashMap JAVA

后端 未结 10 1345
我寻月下人不归
我寻月下人不归 2020-12-02 11:32

I have a Java Property file and there is a KEY as ORDER. So I retrieve the VALUE of that KEY using the getProperty(

10条回答
  •  广开言路
    2020-12-02 11:53

    You can also use JSONObject class from json.org to this will convert your HashMap to JSON string which is well formatted

    Example:

    Map map = new HashMap<>();
    map.put("myNumber", 100);
    map.put("myString", "String");
    
    JSONObject json= new JSONObject(map);
    
    String result= json.toString();
    
    System.out.print(result);
    

    result:

    {'myNumber':100, 'myString':'String'}
    

    Your can also get key from it like

    System.out.print(json.get("myNumber"));
    

    result:

    100
    

提交回复
热议问题