使用Gsonを取得する
JSON
{
"country": "United States",
"mainUrl": "http://www.espn.com",
"outlets": [
{
"country": "U.S.A.",
"displayName": "ESPN",
"sourceUrl": "http://www.espn.com"
},
{
"country": "U.S.A.",
"displayName": "CNN",
"sourceUrl": "http://www.cnn.com"
},
{
"country": "U.S.A.",
"displayName": "Fox News",
"sourceUrl": "http://www.foxnews.com"
},
{
"country": "U.S.A.",
"displayName": "Comcast Sports Network",
"sourceUrl": "http://www.csn.com"
},
{
"country": "U.S.A.",
"displayName": "Yahoo",
"sourceUrl": "http://www.yahoo.com"
},
{
"country": "U.S.A.",
"displayName": "Google News",
"sourceUrl": "http://www.googlenews.com"
}
]
}
方法。あなたはこのようにPOJOを作成する必要があります。
public class Outlet {
@SerializedName("country")
@Expose
private String country;
@SerializedName("displayName")
@Expose
private String displayName;
@SerializedName("sourceUrl")
@Expose
private String sourceUrl;
/**
*
* @return
* The country
*/
public String getCountry() {
return country;
}
/**
*
* @param country
* The country
*/
public void setCountry(String country) {
this.country = country;
}
/**
*
* @return
* The displayName
*/
public String getDisplayName() {
return displayName;
}
/**
*
* @param displayName
* The displayName
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
/**
*
* @return
* The sourceUrl
*/
public String getSourceUrl() {
return sourceUrl;
}
/**
*
* @param sourceUrl
* The sourceUrl
*/
public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}
}
public class Respose {
@SerializedName("country")
@Expose
private String country;
@SerializedName("mainUrl")
@Expose
private String mainUrl;
@SerializedName("outlets")
@Expose
private List<Outlet> outlets = new ArrayList<Outlet>();
/**
*
* @return
* The country
*/
public String getCountry() {
return country;
}
/**
*
* @param country
* The country
*/
public void setCountry(String country) {
this.country = country;
}
/**
*
* @return
* The mainUrl
*/
public String getMainUrl() {
return mainUrl;
}
/**
*
* @param mainUrl
* The mainUrl
*/
public void setMainUrl(String mainUrl) {
this.mainUrl = mainUrl;
}
/**
*
* @return
* The outlets
*/
public List<Outlet> getOutlets() {
return outlets;
}
/**
*
* @param outlets
* The outlets
*/
public void setOutlets(List<Outlet> outlets) {
this.outlets = outlets;
}
}
次に、あなたが問題を示すために[MCVE]を囲み、このコード
Gson gson = new Gson();
Response response = gson.fromJson(json, Response.class);
を使用して
Response.class
に応答を解析することができます。 – t0mm13bあなたの問題を説明するコードを書いてください –
申し訳ありません。私は構成のサイズを確認して、行が戻ってきていることを確認します。私は誰ですか。 – AndroidDev21921