2016-09-07 12 views
0

私は非同期要求のためにRetrofitライブラリを使用していますが、その前にJsonをJavaオブジェクトに変換する必要があります。私はチュートリアルをほとんど見ておらず、理解できました。私はその後、私はゲッターを作るためにどのように混乱していますし、セッターはシリアル化を使用したRetrofit 2.1のJSONオブジェクト

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

やゲッターを作成する方法結果の配列からオブジェクトのJSONリンクはhttps://nomadlist.com/api/v2/list/cities/mumbai-india/places/work

で、Nomadlist APIを使用して自分でプロジェクトを行うことを決めたとセッター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他。そして、メインモデルクラス(私はビジネスを使用)でそのオブジェクトを作成します。

0

このフォーマットを試してみてください:

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

を作成するPOJOクラス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 JsonResponse.Class

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 { 

    } 

を作成0123に行くあなたのレスポンスを越えて、ソースタイプを選択してください:Json and annotation style注釈スタイル:右側にパッケージ名とクラス名を入力してくださいヒットプレビュー最終的にモデルクラスを取得します

関連する問題