티스토리 뷰
MainActivity.kt
package com.jwsoft.kotlinproject
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import org.json.JSONException
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
try {
val jsonString = "{" +
" 'name':'James Kim'," +
" 'age':30," +
" 'hobby':['music', 'movie']," +
" 'privacy':{ 'height':184, 'weight':84 }" +
" }"
var jsonObject = JSONObject(jsonString)
Log.e("jsonObject", jsonObject.toString())
Log.e("name", jsonObject.getString("name"))
Log.e("age", jsonObject.getInt("age").toString())
var hobbyArray = jsonObject.getJSONArray("hobby")
Log.e("hobby", hobbyArray.toString())
for (i in 0 until hobbyArray.length()) {
Log.e("hobby $i", hobbyArray.get(i).toString())
}
var privacyObject = jsonObject.getJSONObject("privacy")
Log.e("privacy", privacyObject.toString())
Log.e("height", privacyObject.getInt("height").toString())
Log.e("weight", privacyObject.getInt("weight").toString())
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
try {
var jsonObject = JSONObject()
jsonObject.put("name", "James Kim")
jsonObject.put("age", 30)
var hobbyArray = JSONArray()
hobbyArray.put("music")
hobbyArray.put("movie")
jsonObject.put("hobby", hobbyArray)
var privacyObject = JSONObject()
privacyObject.put("height", 184)
privacyObject.put("weight", 84)
jsonObject.put("privacy", privacyObject)
Log.e("jsonObject", jsonObject.toString())
Log.e("name", jsonObject.getString("name"))
Log.e("age", jsonObject.getInt("age").toString())
var arrHobby = jsonObject.getJSONArray("hobby")
Log.e("hobby", arrHobby.toString())
for (i in 0 until arrHobby.length()) {
Log.e("hobby $i", arrHobby.get(i).toString())
}
var jsonPrivacy = jsonObject.getJSONObject("privacy")
Log.e("privacy", jsonPrivacy.toString())
Log.e("height", jsonPrivacy.getInt("height").toString())
Log.e("weight", jsonPrivacy.getInt("weight").toString())
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] Retrofit2 + Coroutine (1) | 2020.08.02 |
---|---|
[Kotlin] import com.google.gson.* (0) | 2020.07.31 |
[Kotlin] MVVM + Coroutine (0) | 2020.07.30 |
[Kotlin] Coroutine + CoroutineExceptionHandler (0) | 2020.07.29 |
[Kotlin] Coroutine + ViewModel (0) | 2020.07.29 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java
- Intent
- activity
- recyclerview
- James Kim
- 안드로이드
- 안드로이드 #코틀린 #Android #Kotlin
- DataBinding
- TabLayout
- fragment
- JSONArray
- Livedata
- ArrayList
- ViewModel
- CoordinatorLayout
- 자바
- Android
- JSONObject
- Kotlin
- Architecture Pattern
- MVVM
- Design Pattern
- 혀가 길지 않은 개발자
- ViewPager2
- coroutine
- XML
- 코틀린
- Vue.js #Vue.js + javascript
- handler
- View
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함