Java 使用 Jackson 将 Map 转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29340383/
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-11 07:48:02  来源:igfitidea点击:

Convert Map to JSON using Hymanson

javajsonspringHymansongson

提问by PacificNW_Lover

How can I convert a Map to a valid JSON using Hymanson?

如何使用 Hymanson 将 Map 转换为有效的 JSON?

I am doing it using Google's GSON via a Spring Boot REST Post method...

我正在通过 Spring Boot REST Post 方法使用 Google 的 GSON 来做这件事...

Here's the RESTful Web Service:

这是 RESTful Web 服务:

import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.Hymanson.core.JsonFactory;
import com.fasterxml.Hymanson.core.JsonParser;
import com.google.gson.Gson;

@RestController
@RequestMapping("/myservice")
public class ValidationService {    

    @RequestMapping(value="/validate", method = RequestMethod.POST)
    public void validate(@RequestBody Map<String, Object> payload) throws Exception {
        Gson gson = new Gson();
        String json = gson.toJson(payload); 
        System.out.println(json);
    }
}

So, when I invoke it using this:

所以,当我使用这个调用它时:

curl -H "Accept: application/json" -H "Content-type: application/json" \
-X POST -d '{"name":"value"}' http://localhost:8080/myservice/validate

Receive the following to stdout (this is exactly what I want):

将以下内容接收到标准输出(这正是我想要的):

{"name":"value"}

Is there a better way to do this using Hymanson instead of Google's Gson and / or am I going about it the wrong way altogether?

有没有更好的方法来使用 Hymanson 而不是 Google 的 Gson 和/或者我是否完全以错误的方式来解决这个问题?

回答by Arpit Aggarwal

You should prefer Object Mapper instead. Here is the link for the same : Object Mapper - Spring MVC way of Obect to JSON

您应该更喜欢 Object Mapper。这是相同的链接:Object Mapper - Spring MVC way of Obect to JSON

回答by Mithun

You can convert Mapto JSONusing Hymanson as follows:

您可以按如下方式转换MapJSON使用 Hymanson:

Map<String,String> payload = new HashMap<>();
payload.put("key1","value1");
payload.put("key2","value2");

String json = new ObjectMapper().writeValueAsString(payload);
System.out.println(json);

回答by sebster

If you're using Hymanson, better to convert directly to ObjectNode.

如果您使用的是 Hymanson,最好直接转换为 ObjectNode。

//not including SerializationFeatures for brevity
static final ObjectMapper mapper = new ObjectMapper();

//pass it your payload
public static ObjectNode convObjToONode(Object o) {
    StringWriter stringify = new StringWriter();
    ObjectNode objToONode = null;

    try {
        mapper.writeValue(stringify, o);
        objToONode = (ObjectNode) mapper.readTree(stringify.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(objToONode);
    return objToONode;
}

回答by Uzair

Using Hymanson, you can do it as follows:

使用 Hymanson,您可以按如下方式进行:

    ObjectMapper mapper = new ObjectMapper();
    String clientFilterJson = "";
    try {
        clientFilterJson = mapper.writeValueAsString(filterSaveModel);
    } catch (IOException e) {
        e.printStackTrace();
    }