2016-09-07 2 views
0

비동기 요청을 위해 Retrofit Library를 사용하고 있습니다.하지만 Json을 Java 객체로 변환해야합니다. 나는 약간의 튜토리얼을 보았고 이해할 수 있었다. 그때 나는 내 자신의 사용 Nomadlist API에 대한 프로젝트를하기로 결정, json으로 링크 https://nomadlist.com/api/v2/list/cities/mumbai-india/places/work직렬화를 사용하는 Retrofit 2.1의 JSON 객체

내가 게터를 만드는 방법 혼란 스러워요이며, 호텔은

"updated":{"epoch":1473220041,"time":"2016-09-07T03:47:21+00:00","cache":false} 

객체 또는 결과 배열에서 게터를 만드는 방법과 세터 for

"city":{"name":"Thane","slug":"thane-india","url":"\/thane-india"}. 

나는 다음과 같은 클래스를 만들었습니다.

public class City { 

@SerializedName("name") 
private String nameNmd; 

public String getNameNmd() { 
    return nameNmd; 
} 


@SerializedName("img") 
private String imgNmd; 

public String getImgNmd() { 
    return imgNmd; 
} 

@SerializedName("url") 
private String urlNmd; 

public String getUrlNmd() { 
    return urlNmd; 
} 

@SerializedName("type") 
private String typeNmd; 

public String getTypeNmd() { 
    return typeNmd; 
} 
} 

enter image description here

나는 내가 사용하고 JSON의 부분의 스크린 샷을 추가했습니다. 도시 국가 및 위치 섹션의 올바른 형식은 무엇입니까?

+0

시도 [jsonschema2pojo] (http://www.jsonschema2pojo.org) /) json에서 Java 객체를 직접 생성 – Rehan

답변

0

다음 형식을 사용하여 필요를 처리 할 수 ​​있습니다.

public class City { 
    @SerializedName("name") 
    String name; 
    @SerializedName("slug") 
    String slug; 
    @SerializedName("url") 
    String url; 
} 

public class Country { 

} 

public class Business { 
    @SerializedName("name") 
    String name; 
    @SerializedName("img") 
    String img; 
    @SerializedName("url") 
    String url; 
    @SerializedName("type") 
    String type; 
    @SerializedName("city") 
    City city; 
    @SerializedName("country") 
    Country country; 
} 

City, Country 및 기타에 대해 별도의 클래스를 만듭니다 JSONObject 당신은 당신의 응용 프로그램이 필요합니다. 그리고 기본 모델 클래스에서 객체를 만듭니다 (저는 Business를 사용했습니다).

0

이 형식을 시도해보십시오

public class res { 
     @SerializedName("name") 
     String name; 
     @SerializedName("img") 
     String img; 
     @SerializedName("url") 
     String url; 
     @SerializedName("type") 
     String type; 

     @SerializedName("country") 
     Country country; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public String getImg() { 
      return img; 
     } 

     public void setImg(String img) { 
      this.img = img; 
     } 

     public String getUrl() { 
      return url; 
     } 

     public void setUrl(String url) { 
      this.url = url; 
     } 

     public String getType() { 
      return type; 
     } 

     public void setType(String type) { 
      this.type = type; 
     } 

     public Country getCountry() { 
      return country; 
     } 

     public void setCountry(Country country) { 
      this.country = country; 
     } 
    } 

    public class Country { 

    } 

POJO 클래스를 만들

POJO JsonResponse.Class을 만듭니다 Result.class

 public class Result{ 

    @SerializedName("name") 
    String name; 
    @SerializedName("img") 
    String img; 
    @SerializedName("url") 
    String url; 
    @SerializedName("type") 
    String type; 
    @SerializedName("result") 
    ArrayList<Result> result; 

    @SerializedName("country") 
    Country country; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getImg() { 
     return img; 
    } 

    public void setImg(String img) { 
     this.img = img; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public ArrayList<Result> getResult() { 
     return result; 
    } 

    public void setResult(ArrayList<Result> result) { 
     this.result = result; 
    } 

    public Country getCountry() { 
     return country; 
    } 

    public void setCountry(Country country) { 
     this.country = country; 


} 
} 

만들 POJO 클래스 Updated.class

public class Updated{ 
     @SerializedName("epoch") 
     String epoch; 
     @SerializedName("time") 
     String time; 
     @SerializedName("ache") 
     String ache; 
    public String getEpoch() { 
    return epoch; 
} 

public void setEpoch(String epoch) { 
    this.epoch = epoch; 
} 

public String getTime() { 
    return time; 
} 

public void setTime(String time) { 
    this.time = time; 
} 

public String getAche() { 
    return ache; 
} 

public void setAche(String ache) { 
    this.ache = ache; 
} 

    } 
0

0123로 이동답장을 지난 후 소스 유형 : Json 및 주석 스타일 주석 스타일 선택 : 오른쪽에서 패키지 및 클래스 이름 제공 미리보기 마침내 모델 클래스를 가져옵니다.

관련 문제