Java에서 Map/HashMap을 JSONObject로 변환하는 방법은 무엇입니까? [4가지 다른 방법]
게시 됨: 2019-01-13 개체를 한 형식에서 다른 형식으로 변환하는 것은 일반적인 요청입니다. Java Map/HashMap을 JSONObject로 변환 There are 4 different ways
.
이 자습서에서는 HashMap을 JSONObject로 변환하는 방법에 대해 자세히 설명합니다.
시작하자:
CrunchifyMapToJsonObject.java 클래스를 생성합니다.
방법-1
먼저 Google GSON dependency
을 사용하여 HashMap을 JSONObject로 변환합니다. 프로젝트에 Maven 종속성이 필요합니다.
1 2 3 4 5 |
< dependency > < groupId > com . google . code . gson < / groupId > < artifactId > gson < / artifactId > < version > 2.8.0 < / version > < / dependency > |
방법-2
다음으로 new JSONObject()를 사용하여 org.json dependency
을 사용합니다.
1 2 3 4 5 |
< dependency > < groupId > org . json < / groupId > < artifactId > json < / artifactId > < version > 20180130 < / version > < / dependency > |
방법-3
ObjectMapper().writeValueAsString() 작업과 함께 jackson-core dependency
사용.
1 2 3 4 5 6 7 8 9 10 11 12 |
< dependency > < groupId > com . fasterxml . jackson . core < / groupId > < artifactId > jackson - core < / artifactId > < version > 2.9.5 < / version > < scope > compile < / scope > < / dependency > < dependency > < groupId > com . fasterxml . jackson . core < / groupId > < artifactId > jackson - databind < / artifactId > < version > 2.9.5 < / version > < scope > compile < / scope > < / dependency > |
방법-4
JSONValue.toJSONString() 라이브러리와 함께 json-simple dependency
사용.
1 2 3 4 5 |
< dependency > < groupId > com . googlecode . json - simple < / groupId > < artifactId > json - simple < / artifactId > < version > 1.1.1 < / version > < / dependency > |
위의 모든 maven 종속성을 Java J2EE 프로젝트에 추가해야 합니다. maven 프로젝트가 없다면 다음 단계를 따르십시오.
다음은 완전한 예입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package crunchify . com . tutorial ; import java . util . HashMap ; import java . util . Map ; import org . json . JSONObject ; import org . json . simple . JSONValue ; import com . fasterxml . jackson . core . JsonProcessingException ; import com . fasterxml . jackson . databind . ObjectMapper ; import com . google . gson . Gson ; import com . google . gson . GsonBuilder ; /** * @author Crunchify.com * Program: 4 Best ways to convert Java Map to JSONObject. * Version: 1.0.0 * */ public class CrunchifyMapToJsonObject { public static void main ( String a [ ] ) { Map < String , String > crunchifyMap = new HashMap < String , String > ( ) ; crunchifyMap . put ( "Google" , "San Jose" ) ; crunchifyMap . put ( "Facebook" , "Mountain View" ) ; crunchifyMap . put ( "Crunchify" , "NYC" ) ; crunchifyMap . put ( "Twitter" , "SFO" ) ; crunchifyMap . put ( "Microsoft" , "Seattle" ) ; log ( "Raw Map ===> " + crunchifyMap ) ; // Use this builder to construct a Gson instance when you need to set configuration options other than the default. GsonBuilder gsonMapBuilder = new GsonBuilder ( ) ; Gson gsonObject = gsonMapBuilder . create ( ) ; String JSONObject = gsonObject . toJson ( crunchifyMap ) ; log ( "\nMethod-1: Using Google GSON ==> " + JSONObject ) ; Gson prettyGson = new GsonBuilder ( ) . setPrettyPrinting ( ) . create ( ) ; String prettyJson = prettyGson . toJson ( crunchifyMap ) ; log ( "\nPretty JSONObject ==> " + prettyJson ) ; // Construct a JSONObject from a Map. JSONObject crunchifyObject = new JSONObject ( crunchifyMap ) ; log ( "\nMethod-2: Using new JSONObject() ==> " + crunchifyObject ) ; try { // Default constructor, which will construct the default JsonFactory as necessary, use SerializerProvider as its // SerializerProvider, and BeanSerializerFactory as its SerializerFactory. String objectMapper = new ObjectMapper ( ) . writeValueAsString ( crunchifyMap ) ; log ( "\nMethod-3: Using ObjectMapper().writeValueAsString() ==> " + objectMapper ) ; } catch ( JsonProcessingException e ) { e . printStackTrace ( ) ; } // Convert an object to JSON text. If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly. String jsonValue = JSONValue . toJSONString ( crunchifyMap ) ; log ( "\nMethod-4: Using JSONValue.toJSONString() ==> " + jsonValue ) ; } private static void log ( Object print ) { System . out . println ( print ) ; } } |
위의 프로그램을 Java 응용 프로그램으로 실행하면 아래 출력이 표시되어야 합니다.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Raw Map === > { Google = San Jose , Twitter = SFO , Microsoft = Seattle , Facebook = Mountain View , Crunchify = NYC } Method - 1 : Using Google GSON == > { "Google" : "San Jose" , "Twitter" : "SFO" , "Microsoft" : "Seattle" , "Facebook" : "Mountain View" , "Crunchify" : "NYC" } Pretty JSONObject == > { "Google" : "San Jose" , "Twitter" : "SFO" , "Microsoft" : "Seattle" , "Facebook" : "Mountain View" , "Crunchify" : "NYC" } Method - 2 : Using new JSONObject ( ) == > { "Google" : "San Jose" , "Twitter" : "SFO" , "Microsoft" : "Seattle" , "Facebook" : "Mountain View" , "Crunchify" : "NYC" } Method - 3 : Using ObjectMapper ( ) . writeValueAsString ( ) == > { "Google" : "San Jose" , "Twitter" : "SFO" , "Microsoft" : "Seattle" , "Facebook" : "Mountain View" , "Crunchify" : "NYC" } Method - 4 : Using JSONValue . toJSONString ( ) == > { "Google" : "San Jose" , "Twitter" : "SFO" , "Microsoft" : "Seattle" , "Facebook" : "Mountain View" , "Crunchify" : "NYC" } |
Java Map을 JSONObject로 변환하는 다른 방법을 알고 있다면 아래 의견으로 저희와 모든 독자에게 알려주십시오.
즐거운 코딩.