Java 从 LinkedHashMap 转换为 Json 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22870584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:35:00  来源:igfitidea点击:

Convert from LinkedHashMap to Json String

javajsonmongodblinkedhashmap

提问by user1655510

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

我正在使用 Jongo 与 Mongo 一起工作,当我进行查询时,我收到了一个LinkedHashMap结果。

Iterator one = (Iterator) friends.find(query).projection("{_id:0}").as(Object.class);
while (one.hasNext()) {
    LinkedHashMap data = new LinkedHashMap();
    data = (LinkedHashMap) one.next();
    String content = data.toString();
}

the problem is that if the json is {"user":"something"}content will be {user=something}, it is not a jsonis only toStringmethod from HashMap.

问题是,如果JSON是{"user":"something"}内容将是{user=something},它不是一个json仅仅是toString从方法HashMap

How I can get the original JSON?

我怎样才能得到原件JSON

I don't have a classto map the responseand it isn't a solution create a mapclass, that is why I use a Object.class.

我没有class映射,response也不是创建map类的解决方案,这就是为什么我使用Object.class.

采纳答案by ethanbustad

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

如果您可以访问某些 JSON 库,那似乎就是要走的路。

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

如果使用 org.json 库,请使用public JSONObject(java.util.Map map)

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

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

如果是 Gson,使用gson.toJson()@hellboy 提到的方法:

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

回答by hellboy

You can use Gson library from Google to convert any object to JSON. Here is an example to convert LinkedHashMap to json -

您可以使用 Google 的 Gson 库将任何对象转换为 JSON。这是将 LinkedHashMap 转换为 json 的示例 -

Gson gson = new Gson();
String json = gson.toJson(map,LinkedHashMap.class);

回答by user1655510

I resolved the problem using the following code:

我使用以下代码解决了问题:

    Iterator one = (Iterator) friends.find(query).projection("{_id:0}").as(Object.class);
    while (one.hasNext()) {
        Map data= new HashMap();

        data= (HashMap) one.next();
        JSONObject d = new JSONObject();
        d.putAll(data);
        String content=d.toString();
    }

回答by jarry_dk

One of the com.mongodb.BasicDBObject constructors takes a Map as input. Then you just have to call the toString() on the BasicDBObject object.

com.mongodb.BasicDBObject 构造函数之一将 Map 作为输入。然后你只需要在 BasicDBObject 对象上调用 toString() 。

Iterator one = (Iterator) friends.find(query).projection("{_id:0}").as(Object.class);
    while (one.hasNext()) {
        LinkedHashMap data= new LinkedHashMap();

        data= (LinkedHashMap) one.next();

        com.mongodb.BasicDBObject bdo = new com.mongodb.BasicDBObject(data);    
        String json = bdo.toString();
    }

回答by André Luís Tomaz Dionisio

String content = data.toString()
.replaceAll("=", "\":\"")
.replaceAll(",", "\",\"")
.replaceAll("}", "\"}")
.replaceAll("{", "{\"");