티스토리 뷰
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.java
package com.jwsoft.javaproject;
public class Privacy {
private int height;
private int weight;
public Privacy(int height, int weight) {
this.height = height;
this.weight = weight;
}
}
User.java
package com.jwsoft.javaproject;
public class User {
private String name;
private int age;
private String[] hobby;
private Privacy privacy;
public User(String name, int age, String[] hobby, Privacy privacy) {
this.name = name;
this.age = age;
this.hobby = hobby;
this.privacy = privacy;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// user 1
String jsonString = "{ 'name':'James Kim', 'age':30 }";
Gson gson = new GsonBuilder().create(); // = new Gson()
User user = gson.fromJson(jsonString, User.class); // hobby, privacy 없으면 제외하고 삽입됨.
Log.e("name", user.getName());
Log.e("age", Integer.toString(user.getAge()));
String jsonUser = gson.toJson(user);
Log.e("jsonUser", jsonUser);
// user 2
String[] arr = {"music", "movie"};
Privacy privacy = new Privacy(184, 84);
User user2 = new User("Jason Park", 33, arr, privacy);
String jsonUser2 = gson.toJson(user2);
Log.e("jsonUser2", jsonUser2);
user2 = gson.fromJson(jsonUser2, User.class);
Log.e("name", user2.getName());
Log.e("age", Integer.toString(user2.getAge()));
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
Map<String, Object> userMap = new HashMap<>();
userMap.put("name", "James Kim");
userMap.put("age", 30);
String[] arrHobby = {"music", "movie"};
userMap.put("hobby", arrHobby);
Privacy privacy = new Privacy(184, 84);
userMap.put("privacy", privacy);
String jsonUser = gson.toJson(userMap);
Log.e("jsonUser", jsonUser);
Map map = gson.fromJson(jsonUser, Map.class);
Log.e("map", map.toString());
User user = gson.fromJson(jsonUser, User.class);
Log.e("name", user.getName());
Log.e("age", Integer.toString(user.getAge()));
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "James Kim");
jsonObject.addProperty("age", 30);
JsonArray hobbyArray = new JsonArray();
hobbyArray.add("music");
hobbyArray.add("movie");
jsonObject.add("hobby", hobbyArray);
JsonObject privacy = new JsonObject();
privacy.addProperty("height", 184);
privacy.addProperty("weight", 84);
jsonObject.add("privacy", privacy);
Log.e("jsonObject", jsonObject.toString());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonUser = gson.toJson(jsonObject);
Log.e("jsonUser", jsonUser);
}
}
Gson gson = new Gson(); 에서
Gson gson = new GsonBuilder().setPrettyPrinting().create(); 로 수정
콘솔 출력 시 예쁘게 출력됨.
'Android > Java' 카테고리의 다른 글
[Java] ImageView 타원화 (0) | 2020.08.04 |
---|---|
[Java] CoordinatorLayout + ViewPager2 + TabLayout (0) | 2020.08.03 |
[Java] import org.json.* (0) | 2020.07.30 |
[Java] Thread.join() (0) | 2020.07.27 |
[Java] Synchronized (0) | 2020.07.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- activity
- 안드로이드
- MVVM
- JSONArray
- ViewModel
- 자바
- Intent
- Vue.js #Vue.js + javascript
- recyclerview
- DataBinding
- 안드로이드 #코틀린 #Android #Kotlin
- JSONObject
- TabLayout
- ArrayList
- 코틀린
- XML
- CoordinatorLayout
- Livedata
- java
- 혀가 길지 않은 개발자
- Android
- James Kim
- fragment
- View
- Kotlin
- Architecture Pattern
- handler
- ViewPager2
- Design Pattern
- coroutine
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함