* MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Student student = new Student(1, "김하나", 90, 90, 90);
// object > json
Gson gson = new Gson();
String gsonObject = gson.toJson(student);
println("gson Object : " + gsonObject);
HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
hashmap.put(1, "김하나");
hashmap.put(2, "김두나");
hashmap.put(3, "김세나");
// hashmap > json
Gson gson2 = new Gson();
String gsonHashmap = gson2.toJson(hashmap);
println("gson Hashmap : " + gsonHashmap);
// json > Object
Gson gson3 = new Gson();
Student gsonJson = gson3.fromJson(gsonObject, Student.class);
println("번호 : " + gsonJson.getNum() + ", 이름 : " + gsonJson .getName());
}
public void println(String data) {
Log.d("MainActivity", data);
}
}
* Gradle
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
'android' 카테고리의 다른 글
갤러리 이미지 가져오기 (0) | 2020.02.05 |
---|---|
SQLite 사용법 (0) | 2020.02.05 |
Volley 사용법 (0) | 2020.02.04 |
HttpURLConnection 사용법 (0) | 2020.02.04 |
Socket 통신(Thread 필수) (0) | 2020.02.04 |