티스토리 뷰
build.gradle (Module: app)
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
// com.google.gson
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
}
Privacy.kt
package com.jwsoft.kotlinproject
data class Privacy (
var height: Int,
var weight: Int
)
User.kt
package com.jwsoft.kotlinproject
data class User (
var name: String,
var age: Int,
var hobby: Array<String>,
var privacy: Privacy
)
MainActivity.kt
package com.jwsoft.kotlinproject
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// user 1
val jsonString = "{" +
" 'name':'James Kim'," +
" 'age':30" +
" }"
val gson = Gson()
var user: User = gson.fromJson(jsonString, User::class.java)
Log.e("name", user.name)
Log.e("age", user.age.toString())
var jsonUser = gson.toJson(user)
Log.e("jsonUser", jsonUser)
// user 2
var arrHobby = arrayOf("music", "movice")
var privacy = Privacy(184, 84)
var user2 = User("Sonata Lee", 27, arrHobby, privacy)
var jsonUser2 = gson.toJson(user2)
Log.e("jsonUser2", jsonUser2)
var user3: User = gson.fromJson(jsonUser2, User::class.java)
Log.e("name", user3.name)
Log.e("age", user3.age.toString())
for (i in 0 until user3.hobby.size) {
Log.e("hobby $i", user3.hobby[i])
}
Log.e("privacy", user3.privacy.toString())
Log.e("height", user3.privacy.height.toString())
Log.e("weight", user3.privacy.weight.toString())
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.GsonBuilder
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var userMap: HashMap<String, Any> = HashMap()
userMap["name"] = "James Kim"
userMap["age"] = 30
val arrHobby: Array<String> = arrayOf("music", "movie")
userMap["hobby"] = arrHobby
val privacy = Privacy(184, 84)
userMap["privacy"] = privacy
val gson = GsonBuilder().create()
val jsonUser = gson.toJson(userMap)
Log.e("jsonUser", jsonUser)
var map = gson.fromJson(jsonUser, HashMap::class.java)
Log.e("map", map.toString())
var user = gson.fromJson(jsonUser, User::class.java)
Log.e("name", user.name)
Log.e("age", user.age.toString())
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonObject
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var jsonObject: JsonObject = JsonObject()
jsonObject.addProperty("name", "James Kim")
jsonObject.addProperty("age", 30)
var hobbyArray = JsonArray()
hobbyArray.add("music")
hobbyArray.add("movie")
jsonObject.add("hobby", hobbyArray)
var privacy = JsonObject()
privacy.addProperty("height", 184)
privacy.addProperty("weight", 84)
jsonObject.add("privacy", privacy)
Log.e("jsonObject", jsonObject.toString())
val gson = GsonBuilder().setPrettyPrinting().create()
val jsonString = gson.toJson(jsonObject)
Log.e("jsonString", jsonString)
val user = gson.fromJson(jsonObject, User::class.java)
Log.e("name", user.name)
Log.e("age", user.age.toString())
}
}
val gson = Gson() 에서
val gson = GsonBuilder().setPrettyPrinting().create() 로 수정
콘솔 출력 시 예쁘게 출력됨.
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] CoordinatorLayout + ViewPager2 + TabLayout (0) | 2020.08.03 |
---|---|
[Kotlin] Retrofit2 + Coroutine (1) | 2020.08.02 |
[Kotlin] import org.json.* (0) | 2020.07.31 |
[Kotlin] MVVM + Coroutine (0) | 2020.07.30 |
[Kotlin] Coroutine + CoroutineExceptionHandler (0) | 2020.07.29 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DataBinding
- 안드로이드 #코틀린 #Android #Kotlin
- activity
- ArrayList
- Design Pattern
- CoordinatorLayout
- fragment
- JSONObject
- MVVM
- Livedata
- ViewModel
- Kotlin
- XML
- recyclerview
- View
- JSONArray
- handler
- TabLayout
- James Kim
- 코틀린
- coroutine
- Intent
- 혀가 길지 않은 개발자
- Android
- ViewPager2
- 안드로이드
- 자바
- Vue.js #Vue.js + javascript
- java
- Architecture Pattern
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함