How to convert hashmap to JSON object in Java

前端 未结 29 2348
谎友^
谎友^ 2020-11-22 11:20

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

29条回答
  •  孤城傲影
    2020-11-22 11:35

    If you are using net.sf.json.JSONObject then you won't find a JSONObject(map) constructor in it. You have to use the public static JSONObject fromObject( Object object ) method. This method accepts JSON formatted strings, Maps, DynaBeans and JavaBeans.

    JSONObject jsonObject = JSONObject.fromObject(myMap);

提交回复
热议问题