将Jackson对象转换为JSONObject java

时间:2021-07-12 18:03:44

I'm trying to figure out how to convert a Jackson object into a JSONObject?

我想弄清楚如何将Jackson对象转换为JSONObject?

What I've tried, however I don't believe this is the correct approach.

我尝试了什么,但我不相信这是正确的方法。

public JSONObject toJSON() throws IOException {
    ObjectMapper mapper = new ObjectMapper();       
    return new JSONObject(mapper.writeValueAsString(new Warnings(warnings)));
}

2 个解决方案

#1


6  

Right now, you are serializing your Pojo to a String, then parsing that String and converting it into a HashMap style object in the form of JSONObject.

现在,您将Pojo序列化为String,然后解析该String并将其转换为JSONObject形式的HashMap样式对象。

This is very inefficient and doesn't accomplish anything of benefit.

这是非常低效的,并没有实现任何好处。

Jackson already provides an ObjectNode class for interacting with your Pojo as a JSON object. So just convert your object to an ObjectNode. Here's a working example

Jackson已经提供了一个ObjectNode类,用于与Pojo作为JSON对象进行交互。所以只需将对象转换为ObjectNode即可。这是一个有效的例子

public class Example {
    public static void main(String[] args) throws Exception {
        Pojo pojo = new Pojo();
        pojo.setAge(42);
        pojo.setName("Sotirios");
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode node = mapper.valueToTree(pojo);
        System.out.println(node);
    }
}

class Pojo {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Otherwise, the way you are doing it is fine.

否则,你这样做的方式很好。

#2


3  

The way you are doing is work fine, because i also use that way to make a JSONobject.

你正在做的工作正常,因为我也使用这种方式来制作一个JSON对象。

here is my code

这是我的代码

 public JSONObject getRequestJson(AccountInquiryRequestVO accountInquiryRequestVO) throws  JsonGenerationException, JsonMappingException, IOException {
          ObjectMapper mapper = new ObjectMapper();
          JSONObject jsonAccountInquiry;

           jsonAccountInquiry=new JSONObject(mapper.writeValueAsString(accountInquiryRequestVO));

  return jsonAccountInquiry;  
 }

its working fine for me. but you can always use JsonNode also here is the sample code for that

它对我来说很好。但你可以随时使用JsonNode,这里是示例代码

JsonNode jsonNode=mapper.valueToTree(accountInquiryRequestVO);

its very easy to use.

它非常容易使用。

#1


6  

Right now, you are serializing your Pojo to a String, then parsing that String and converting it into a HashMap style object in the form of JSONObject.

现在,您将Pojo序列化为String,然后解析该String并将其转换为JSONObject形式的HashMap样式对象。

This is very inefficient and doesn't accomplish anything of benefit.

这是非常低效的,并没有实现任何好处。

Jackson already provides an ObjectNode class for interacting with your Pojo as a JSON object. So just convert your object to an ObjectNode. Here's a working example

Jackson已经提供了一个ObjectNode类,用于与Pojo作为JSON对象进行交互。所以只需将对象转换为ObjectNode即可。这是一个有效的例子

public class Example {
    public static void main(String[] args) throws Exception {
        Pojo pojo = new Pojo();
        pojo.setAge(42);
        pojo.setName("Sotirios");
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode node = mapper.valueToTree(pojo);
        System.out.println(node);
    }
}

class Pojo {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Otherwise, the way you are doing it is fine.

否则,你这样做的方式很好。

#2


3  

The way you are doing is work fine, because i also use that way to make a JSONobject.

你正在做的工作正常,因为我也使用这种方式来制作一个JSON对象。

here is my code

这是我的代码

 public JSONObject getRequestJson(AccountInquiryRequestVO accountInquiryRequestVO) throws  JsonGenerationException, JsonMappingException, IOException {
          ObjectMapper mapper = new ObjectMapper();
          JSONObject jsonAccountInquiry;

           jsonAccountInquiry=new JSONObject(mapper.writeValueAsString(accountInquiryRequestVO));

  return jsonAccountInquiry;  
 }

its working fine for me. but you can always use JsonNode also here is the sample code for that

它对我来说很好。但你可以随时使用JsonNode,这里是示例代码

JsonNode jsonNode=mapper.valueToTree(accountInquiryRequestVO);

its very easy to use.

它非常容易使用。