我们可以使用 toJSONString() 方法将 Map 转换为 JSON 对象( >static) 的 org.json.simple.JSONValue。 它有两个重要的静态方法:writeJSONString()方法将对象编码为JSON文本并将其写出,escape()方法转义特殊字符和转义引号,\、/、\r、\n、\b、\f、\t。
import java.util.*; import org.json.simple.JSONValue; public class ConvertMapJSONTest { public static void main(String[] args) { Map<String, Object> map = new HashMap<String, Object>(); map.put("1", "India"); map.put("2", "Australia"); map.put("3", "England"); map.put("4", "South Africa"); String jsonStr = JSONValue.toJSONString(map); // converts Map to JSON System.out.println(jsonStr); } }
{"1":"India","2":"Australia","3":"England","4":"South Africa"}
以上就是我们如何在Java中将地图转换为JSON对象?的详细内容,更多请关注php中文网其它相关文章!
已抢65124个
抢已抢28589个
抢已抢15452个
抢已抢7752个
抢已抢9654个
抢已抢19646个
抢