Convert from LinkedHashMap to Json String

前端 未结 4 1228
情歌与酒
情歌与酒 2020-12-15 23:40

I\'m Workin with Mongo using Jongo, when I do a query I receive a LinkedHashMap as result.

Iterator one = (Iterator) friends.find(query).project         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 00:15

    If you have access to some JSON library, it seems like that's the way to go.

    If using org.json library, use public JSONObject(java.util.Map map):

    String jsonString = new JSONObject(data).toString()
    

    If Gson, use the gson.toJson() method mentioned by @hellboy:

    String jsonString = new Gson().toJson(data, Map.class);
    

提交回复
热议问题