Cara Ngonversi Peta Java menyang JSON

Ana sawetara cara kanggo ngowahi Peta Java dadi JSON. Wis umume ngonversi Array lan Peta Java dadi JSON lan uga kosok balene.

Ing kiriman iki, kita bakal ndeleng 3 conto sing beda kanggo ngonversi Peta Java menyang JSON. Kita bakal nggunakake perpustakaan Jackson, Gson lan org.json.



Peta Java menyang JSON nggunakake Jackson

Tuladha ing ngisor iki nggunakake Jackson Core lan Jackson Binding kanggo ngowahi Peta Java dadi JSON.


Kanggo nggunakake perpustakaan Jackson, luwih dhisik kudu ditambahake ing pom.xml file:



com.fasterxml.jackson.core

jackson-core
2.9.8


com.fasterxml.jackson.core
jackson-databind
2.9.8

Banjur:


import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; public class ConvertJavaMapToJson {
@Test
public void convertMapToJson() {
Map elements = new HashMap();
elements.put('Key1', 'Value1');
elements.put('Key2', 'Value2');
elements.put('Key3', 'Value3');

ObjectMapper objectMapper = new ObjectMapper();

try {

String json = objectMapper.writeValueAsString(elements);

System.out.println(json);
} catch (JsonProcessingException e) {

e.printStackTrace();
}
} }

Output:



{'Key2':'Value2','Key1':'Value1','Key3':'Value3'}

Kaya sing bisa dideleng saka output, urutan elemen ing JSON ora padha karo urutan sing ditambahake menyang peta.

Kanggo njaga supaya, kita kudu nggunakake SortedMap tinimbang.

tuladhane


SortedMap elements = new TreeMap();

Output:

{'Key1':'Value1','Key2':'Value2','Key3':'Value3'}

Gegandhengan:



Peta Java menyang JSON nggunakake Gson

Tuladha ing ngisor iki nggunakake perpustakaan Gson kanggo ngonversi Peta Java dadi JSON, nanging luwih dhisik, kudu nambah Gson minangka katergantungan pom.xml ngajukake



com.google.code.gson
gson
2.8.5

Banjur:


import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.junit.jupiter.api.Test; import java.lang.reflect.Type; import java.util.HashMap; import java.util.SortedMap; import java.util.TreeMap; public class ConvertJavaMapToJson {
@Test
public void convertMapToJson() {
SortedMap elements = new TreeMap();
elements.put('Key1', 'Value1');
elements.put('Key2', 'Value2');
elements.put('Key3', 'Value3');

Gson gson = new Gson();
Type gsonType = new TypeToken(){}.getType();
String gsonString = gson.toJson(elements,gsonType);
System.out.println(gsonString);
} }

Output:

{'Key1':'Value1','Key2':'Value2','Key3':'Value3'}

Peta Java menyang JSON nggunakake org.json

Tuladha ing ngisor iki nggunakake perpustakaan org.json kanggo ngowahi Peta Java menyang JSON, nanging luwih dhisik, kita kudu nambah org.json minangka katergantungan pom.xml ngajukake



org.json
json
20180813

Banjur:

import org.json.JSONObject; import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; public class ConvertJavaMapToJson {
@Test
public void convertMapToJson() {
Map elements = new HashMap();
elements.put('Key1', 'Value1');
elements.put('Key2', 'Value2');
elements.put('Key3', 'Value3');

JSONObject json = new JSONObject(elements);

System.out.println(json);
} }

Output:


{'Key2':'Value2','Key1':'Value1','Key3':'Value3'}