Me pehea te huri i te Mahere Java ki a JSON

He maha nga huarahi hei huri i te Mahere Java ki a JSON. He mea noa ki te huri i nga Huringa Java me nga Mahere ki te JSON me te rereke.

I tenei panui, ka tirohia nga tauira rereke e 3 hei huri i te Mahere Java ki a JSON. Ka whakamahia e maatau nga whare pukapuka o Jackson, Gson me te org.json.

Mahere Java ki JSON ma Jackson

Ko te tauira e whai ake nei e whakamahi ana i a Jackson Core me Jackson Binding hei huri i te Mahere Java ki a JSON.

Hei whakamahi i nga wharepukapuka o Jackson, me matua taapiri atu ki to pom.xml konae:



com.fasterxml.jackson.core

jackson-core
2.9.8


com.fasterxml.jackson.core
jackson-databind
2.9.8

Na:

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();
}
} }

Putanga:



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

Ka kitea mai i te whakaputanga, ko te raupapa o nga waahanga i roto i te JSON kaore i te rite ki te ota i taapirihia e maatau ki te mapi.

Kia mau tonu te ota, me whakamahi SortedMap hei utu mo

hei tauira

SortedMap elements = new TreeMap();

Putanga:

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

E pā ana:

Mahere Java ki JSON e whakamahi ana i a Gson

Ma te tauira e whai ake nei e whakamahi te whare pukapuka Gson hei huri i te Mahere Java ki a JSON, engari ko te mea tuatahi, me taapiri atu a Gson hei whakawhirinaki ki pom.xml konae



com.google.code.gson
gson
2.8.5

Na:

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);
} }

Putanga:

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

Mahere Java ki JSON ma te whakamahi i te org.json

Ko te tauira e whai ake nei e whakamahi ana i te wharepukapuka org.json hei huri i te Mahere Java ki a JSON, engari ko te mea tuatahi, me tapiri e tatou a org.json hei whakawhirinaki ki pom.xml konae



org.json
json
20180813

Na:

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);
} }

Putanga:

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