这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.jsonobjecttojsonarray;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.json.JSONObject;

public class GsonConverter {

JsonArray convertToKeyValueArray(JSONObject jsonObject) {
JsonArray result = new JsonArray();
jsonObject.keySet().forEach(key -> {
JsonObject entry = new JsonObject();
entry.addProperty("key", key);
entry.add("value", com.google.gson.JsonParser.parseString(jsonObject.get(key).toString()));
result.add(entry);
});
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.jsonobjecttojsonarray;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.json.JSONObject;

public class JacksonConverter {

ArrayNode convertToArray(JSONObject jsonObject) {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.convertValue(jsonObject.toMap(), JsonNode.class);

ArrayNode result = mapper.createArrayNode();
jsonNode.fields().forEachRemaining(entry -> {
ObjectNode obj = mapper.createObjectNode();
obj.put("key", entry.getKey());
obj.set("value", entry.getValue());
result.add(obj);
});

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.jsonobjecttojsonarray;

import org.json.JSONArray;
import org.json.JSONObject;

public class OrgJsonConverter {

JSONArray convertValuesToArray(JSONObject jsonObject) {
return new JSONArray(jsonObject.toMap().values());
}

JSONArray convertToEntryArray(JSONObject jsonObject) {
JSONArray result = new JSONArray();
for (String key : jsonObject.keySet()) {
JSONObject entry = new JSONObject();
entry.put("key", key);
entry.put("value", jsonObject.get(key));
result.put(entry);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.jsonobjecttojsonarray;

import com.google.gson.JsonArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class GsonConverterTest {

@Test
void givenJSONObject_whenConvertToKeyValueArray_thenJsonArrayWithObjects() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("brand", "Tesla");
jsonObject.put("year", 2024);

GsonConverter converter = new GsonConverter();
System.out.println("before :"+jsonObject);
JsonArray result = converter.convertToKeyValueArray(jsonObject);

System.out.println("here :"+result);

assertEquals(2, result.size());
assertEquals("year", result.get(0).getAsJsonObject().get("key").getAsString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.jsonobjecttojsonarray;

import org.json.JSONObject;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class JacksonConverterTest {

@Test
void givenJSONObject_whenConvertToArray_thenArrayNodeOfKeyValueObjects() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("country", "India");
jsonObject.put("code", "IN");

JacksonConverter converter = new JacksonConverter();
ArrayNode result = converter.convertToArray(jsonObject);

assertEquals(2, result.size());
assertEquals("country", result.get(0).get("key").asText());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.jsonobjecttojsonarray;

import org.json.JSONObject;
import org.json.JSONArray;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class OrgJsonConverterTest {

@Test
void givenFlatJSONObject_whenConvertValues_thenJSONArrayOfValues() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 1);
jsonObject.put("name", "Alice");

OrgJsonConverter converter = new OrgJsonConverter();
JSONArray result = converter.convertValuesToArray(jsonObject);

assertEquals(2, result.length());
assertTrue(result.toList().contains("Alice"));
}

@Test
void givenFlatJSONObject_whenConvertToEntryArray_thenJSONArrayOfObjects() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("language", "Java");
jsonObject.put("framework", "Spring");

OrgJsonConverter converter = new OrgJsonConverter();
JSONArray result = converter.convertToEntryArray(jsonObject);

assertEquals(2, result.length());
assertEquals("framework", result.getJSONObject(0).get("key"));
}
}