Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 숫자
- 오버라이드
- sub query
- 스프링
- 게시판
- snowpack
- 답글
- 왕초보
- 미니게임
- 시큐어코딩
- Spinner
- Spring
- FIle
- degit
- React
- git
- Android
- 안드로이드
- 상속
- java#왕초보
- parcel
- kotlin
- 버전일치
- 코틀린
- 함수
- SQL
- webpack
- 스타일보험
- java
- 쿠키
Archives
- Today
- Total
YSHUSH
JSON 본문
JSON을 읽어와서 화면에 띄워보자
1. app - src - main - assets경로 생성&우클릭 - 새로만들기 - 파일 - data.json이름의 파일 생성
data.json
[
{
"id": "kotlin",
"language": "코틀린"
},
{
"id": "java",
"language": "자바"
},
{
"id": "swift",
"language": "스위프트"
}
]
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="379dp"
android:layout_height="195dp"
android:text=""
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintHorizontal_bias="0.502"/>
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 문자열
val jsonStr = assets.open("data.json").reader().readText()
Log.d("jsonStr", jsonStr)
// Json으로 파싱
val jsonArray = JSONArray(jsonStr)
Log.d("jsonStr", jsonArray.toString())
val textView = findViewById<TextView>(R.id.textView)
for (i in 0 until jsonArray.length()){
val jsonObject = jsonArray.getJSONObject(i)
textView.append("\n---------------------\n")
val id = jsonObject.getString("id")
val language = jsonObject.getString("language")
textView.append(
"""
$id
""".trimIndent()
)
textView.append(
"""
$language
""".trimIndent()
)
}
}
}

'Coding > Android(kotlin)' 카테고리의 다른 글
화면 전환(Intent) (0) | 2022.02.10 |
---|---|
화면 전환(Preference) (0) | 2022.02.10 |
File (0) | 2022.02.10 |
Counter(숫자 세기 미니 프로그램) (0) | 2022.02.08 |
Rating bar (0) | 2022.02.08 |