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