如何在Java中将List<com.amazonaws.util.json.JSONObject>转为JSON字符串
Got it, let's walk through how to turn your List<JSONObject> into a JSON string using Jackson's ObjectMapper. I'll cover two common approaches depending on whether you want to add an extra Jackson module or not.
Approach 1: Use Jackson's JSON-Org Module (Simplest)
Jackson has a dedicated module to handle org.json.JSONObject types directly. First, make sure you have the jackson-datatype-json-org dependency in your project. Then, register the module with your ObjectMapper and you're good to go:
// Register the module to handle org.json types objectMapper.registerModule(new JsonOrgModule()); // Serialize the list directly to JSON string String jsonString = objectMapper.writeValueAsString(jsonObjlist);
This is the cleanest method if you don't mind adding the extra dependency.
Approach 2: Convert to List
If you want to avoid adding another module, you can manually convert each JSONObject to a Map<String, Object> before serializing. Here's how:
List<Map<String, Object>> mapList = new ArrayList<>(); for (JSONObject jsonObj : jsonObjlist) { Map<String, Object> map = new HashMap<>(); Iterator<String> keys = jsonObj.keys(); while (keys.hasNext()) { String key = keys.next(); map.put(key, jsonObj.get(key)); } mapList.add(map); } // Now serialize the map list to JSON String jsonString = objectMapper.writeValueAsString(mapList);
This works because Jackson knows how to serialize Map instances out of the box.
Handling Empty Values
I noticed your list has an empty string for mx_Age. By default, Jackson will include this in the output. If you want to exclude empty (or null) values, you can configure your ObjectMapper like this:
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
This will omit any entries where the value is an empty string, null, or an empty collection.
Full Working Example
Here's a complete code snippet that uses Approach 2 (no extra modules) with your exact input list:
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.annotation.JsonInclude; import org.json.JSONObject; import java.util.*; public class JsonConversionExample { public static void main(String[] args) throws Exception { // Your input list List<JSONObject> jsonObjlist = new ArrayList<>(); jsonObjlist.add(new JSONObject("{\"Attribute\":\"EmailAddress\",\"Value\":\"abc@yahoo.com\"}")); jsonObjlist.add(new JSONObject("{\"Attribute\":\"Source\",\"Value\":\"Missing_Fixed\"}")); jsonObjlist.add(new JSONObject("{\"Attribute\":\"mx_Lead_Status\",\"Value\":\"Registered User\"}")); jsonObjlist.add(new JSONObject("{\"Attribute\":\"mx_Age\",\"Value\":\"\"}")); jsonObjlist.add(new JSONObject("{\"Attribute\":\"mx_LoginID\",\"Value\":\"abc@yahoo.com\"}")); jsonObjlist.add(new JSONObject("{\"Attribute\":\"mx_Registration_Source\",\"Value\":\"EMAIL\"}")); // Initialize ObjectMapper ObjectMapper objectMapper = new ObjectMapper(); // Optional: Uncomment to exclude empty values // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // Convert List<JSONObject> to List<Map> List<Map<String, Object>> mapList = new ArrayList<>(); for (JSONObject jsonObj : jsonObjlist) { Map<String, Object> map = new HashMap<>(); Iterator<String> keys = jsonObj.keys(); while (keys.hasNext()) { String key = keys.next(); map.put(key, jsonObj.get(key)); } mapList.add(map); } // Generate JSON string String jsonString = objectMapper.writeValueAsString(mapList); System.out.println(jsonString); } }
Running this will output the JSON string matching your input list. If you enable the NON_EMPTY inclusion, the mx_Age entry will be excluded from the result.
内容的提问来源于stack exchange,提问作者Vinay Sawant

