나는 Gson
(Google에서) 사용하여 해냈습니다 .
모듈에 다음 행을 추가하십시오 build.gradle
.
dependencies {
// ...
// Note that `compile` will be deprecated. Use `implementation` instead.
// See https://stackoverflow.com/a/44409111 for more info
implementation 'com.google.code.gson:gson:2.8.2'
}
JSON
끈:
private String jsonString = "[\n" +
" {\n" +
" \"id\": \"c200\",\n" +
" \"name\": \"Ravi Tamada\",\n" +
" \"email\": \"ravi@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c201\",\n" +
" \"name\": \"Johnny Depp\",\n" +
" \"email\": \"johnny_depp@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c202\",\n" +
" \"name\": \"Leonardo Dicaprio\",\n" +
" \"email\": \"leonardo_dicaprio@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c203\",\n" +
" \"name\": \"John Wayne\",\n" +
" \"email\": \"john_wayne@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c204\",\n" +
" \"name\": \"Angelina Jolie\",\n" +
" \"email\": \"angelina_jolie@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c205\",\n" +
" \"name\": \"Dido\",\n" +
" \"email\": \"dido@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c206\",\n" +
" \"name\": \"Adele\",\n" +
" \"email\": \"adele@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c207\",\n" +
" \"name\": \"Hugh Jackman\",\n" +
" \"email\": \"hugh_jackman@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c208\",\n" +
" \"name\": \"Will Smith\",\n" +
" \"email\": \"will_smith@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c209\",\n" +
" \"name\": \"Clint Eastwood\",\n" +
" \"email\": \"clint_eastwood@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c2010\",\n" +
" \"name\": \"Barack Obama\",\n" +
" \"email\": \"barack_obama@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c2011\",\n" +
" \"name\": \"Kate Winslet\",\n" +
" \"email\": \"kate_winslet@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c2012\",\n" +
" \"name\": \"Eminem\",\n" +
" \"email\": \"eminem@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" }\n" +
" ]";
ContactModel.java
:
public class ContactModel {
public String id;
public String name;
public String email;
}
JSON 문자열을 ArrayList<Model>
다음으로 변환하는 코드 :
참고 : 다음을 가져와야합니다 java.lang.reflect.Type;
.
// Top of file
import java.lang.reflect.Type;
// ...
private void parseJSON() {
Gson gson = new Gson();
Type type = new TypeToken<List<ContactModel>>(){}.getType();
List<ContactModel> contactList = gson.fromJson(jsonString, type);
for (ContactModel contact : contactList){
Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email);
}
}
이것이 당신을 도울 것입니다.