Mule技术问题:如何将数据库查询结果作为JSON数组存入JSON对象
Hey there, let's work through this issue step by step! The error you're hitting happens because the data coming from your database is a JSON-formatted string, not an actual JSON Array object that your Java JSON library can directly work with. You can't just cast that string to a JSON array—you need to first parse it into a proper JSON array structure using your chosen JSON library.
Below are solutions using three of the most common Java JSON libraries. Pick the one that matches what you're using in your project:
1. Using Jackson (com.fasterxml.jackson)
Jackson is widely used in Spring and other Java frameworks. Here's how to handle the parsing and object construction:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; public class JsonBuilder { public static void main(String[] args) throws Exception { // This is the string you get from your database String dbCarsJson = "[{\"Make\":\"Honda\",\"Model\":\"Civic\",\"Price\":\"25000\"},{\"Make\":\"Toyota\",\"Model\":\"Camry\",\"Price\":\"20000\"}]"; ObjectMapper objectMapper = new ObjectMapper(); // Parse the database string into a JsonNode (which represents the JSON array) JsonNode carsJsonArray = objectMapper.readTree(dbCarsJson); // Build your target JSON object ObjectNode targetJsonObject = objectMapper.createObjectNode(); targetJsonObject.put("Name", "Tom"); // Add the Hobbies array targetJsonObject.putArray("Hobbies") .add("Fishing") .add("Reading"); // Attach the parsed Cars array to the target object targetJsonObject.set("Cars", carsJsonArray); // Convert to a pretty-printed JSON string if needed String finalJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(targetJsonObject); System.out.println(finalJson); } }
2. Using Gson (com.google.gson)
Gson is Google's lightweight JSON library. Here's the equivalent code:
import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class GsonJsonBuilder { public static void main(String[] args) { String dbCarsJson = "[{\"Make\":\"Honda\",\"Model\":\"Civic\",\"Price\":\"25000\"},{\"Make\":\"Toyota\",\"Model\":\"Camry\",\"Price\":\"20000\"}]"; // Parse the database string into a JsonArray JsonArray carsJsonArray = JsonParser.parseString(dbCarsJson).getAsJsonArray(); // Build the target JSON object JsonObject targetJsonObject = new JsonObject(); targetJsonObject.addProperty("Name", "Tom"); // Create and add the Hobbies array JsonArray hobbiesArray = new JsonArray(); hobbiesArray.add("Fishing"); hobbiesArray.add("Reading"); targetJsonObject.add("Hobbies", hobbiesArray); // Add the parsed Cars array targetJsonObject.add("Cars", carsJsonArray); // Print the final JSON System.out.println(targetJsonObject.toString()); } }
3. Using org.json (org.json)
This is a simple, lightweight core JSON library:
import org.json.JSONArray; import org.json.JSONObject; public class OrgJsonBuilder { public static void main(String[] args) { String dbCarsJson = "[{\"Make\":\"Honda\",\"Model\":\"Civic\",\"Price\":\"25000\"},{\"Make\":\"Toyota\",\"Model\":\"Camry\",\"Price\":\"20000\"}]"; // Parse the database string directly into a JSONArray JSONArray carsJsonArray = new JSONArray(dbCarsJson); // Build your target JSONObject JSONObject targetJsonObject = new JSONObject(); targetJsonObject.put("Name", "Tom"); // Add the Hobbies array JSONArray hobbiesArray = new JSONArray(); hobbiesArray.put("Fishing"); hobbiesArray.put("Reading"); targetJsonObject.put("Hobbies", hobbiesArray); // Attach the Cars array targetJsonObject.put("Cars", carsJsonArray); // Print with indentation for readability System.out.println(targetJsonObject.toString(2)); } }
Key Takeaway
No matter which library you use, the core fix is:
- Don't try to cast the database's JSON string directly to a JSON array
- Use your library's built-in parsing method to convert the string into a proper JSON array object first
- Then add that parsed array to your target JSON object
内容的提问来源于stack exchange,提问作者jayachsi

