我不知道如何使用改造解析json 。熟悉使用Retrofit解析简单的json但不熟悉使用 Retrofit 解析嵌套的Json 。
这是我的Json数据.................
{
"current_observation": {
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
{
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
}
}
}
}
任何帮助将不胜感激。 谢谢。
这是我的简单json方法
public class Country {
@SerializedName("current_observation")
@Expose
private List<Items> items;
public List<Items> getItems() {
return items;
}
public void setItems (List<Items> items) {
this.items = items;
}
}
来了......
public class Items {
@SerializedName("image")
@Expose
private String url;
private String title;
private String link;
public String getFlag() {
return url;
}
public String getRank() {
return link;
}
public void setRank(String link) {
this.link = link;
}
public void setFlag(String url) {
this.url = url;
}
public String getCountryname() {
return title;
}
public void setCountryname(String rating) {
this.title = rating;
}
}
主要活动中的代码
Call <Country> call = apiInterface.getCountries();
call.enqueue(new Callback <Country>() {
@Override
public void onResponse(Call<Country> call, Response<Country> response) {
Log.d(TAG,"onSuccess Server Response "+ response.toString());
Log.d(TAG,"onSuccess received information "+ response.body().toString());
List<Items> items = response.body().getItems();
adapter = new RecAdapter(items, getContext().getApplicationContext());
recyclerView.setAdapter(adapter);
}
答案 0 :(得分:4)
使用Response
Retrofit
课程下方
class Response{
@SerializedName("current_observation")
Observation observation;
//getters and setters
}
class Observation{
@SerializedName("image")
Image image;
//getters and setters
}
class Image{
@SerializedName("title")
String title;
@SerializedName("link")
String link;
@SerializedName("url")
String url;
//getters and setters
}
答案 1 :(得分:2)
如果正确实现Pojo,这将更容易。
你的班级和你的json有冲突。
来自您的国家/地区类,&#34; current_observation&#34;将是List<Items> items;
然后你的json应该是这样的:
"current_observation":[
{
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
}
},
{
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
}
}]
如果使用List,请在方括号[]
处注意。即使只有一件商品,您的"current_observation"
仍需要申报List<T>
。
我建议您使用本网站:http://www.jsonschema2pojo.org/
选择Source Type: JSON
,Annotation style: Moshi
(我使用Moshi,或者您可以使用Gson),勾选:Make class serialiable
它会为你的json生成正确的Class。你的其余代码应该是正确的。
<强>更新强>: 如果生成类后没有生成List,则不应使用
List<Items> items = response.body().getItems();
但是
Items items = response.body().getItems();
答案 2 :(得分:2)
CurrentObservation.class
public class CurrentObservation {
@SerializedName("image")
@Expose
private Image1 image;
public Image1 getImage() {
return image;
}
public void setImage(Image1 image) {
this.image = image;
}
}
Example.java
public class Example {
@SerializedName("current_observation")
@Expose
private CurrentObservation currentObservation;
public CurrentObservation getCurrentObservation() {
return currentObservation;
}
public void setCurrentObservation(CurrentObservation currentObservation) {
this.currentObservation = currentObservation;
}
}
Image1.java
public class Image1 {
@SerializedName("url")
@Expose
private String url;
@SerializedName("title")
@Expose
private String title;
@SerializedName("link")
@Expose
private String link;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
在主要活动
中调用它 Call<Example> ex = BaseUrlClass.getInterface().ex("whatever parameters");
ex.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
Example list = response.body();
CurrentObservation a = list.getCurrentObservation();
List<Image1> im = a.getImage();
for (int i = 0;i<im.size();i++){
Image1 image1= im.get(i);
String a = image1.getTitle();
String b = image1.getUrl();
String c = image1.getLink();
}
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
}
});
最后在你的界面
public interface ApiUtils {
@GET("") //whatever url
Call<Example> ex(); //or any parameter
}
答案 3 :(得分:1)
对于JSON解析,您应该使用JASONSCHEMA2POJO将JSON字符串转换为模型类
在改装成功回复中
CurrentObservation observation = new CurrentObservation ();
JSONObject jsonObject = new JSONObject(response.body().string());
observation = new Gson().fromJson(jsonObject.getString("current_observation"),CurrentObservation .class);
模型类赞
public class Example {
@SerializedName("current_observation")
@Expose
private CurrentObservation currentObservation;
public CurrentObservation getCurrentObservation() {
return currentObservation;
}
public void setCurrentObservation(CurrentObservation currentObservation) {
this.currentObservation = currentObservation;
}
}
答案 4 :(得分:1)
我如何在主要活动中调用它?
你需要像这样调用pojo类方法
String url=getCurrentObservation().getImage().getUrl();
如果你得到回复
String url=response.body().getCurrentObservation().getImage().getUrl();
要获得更多帮助,请使用我的ans
关注this link答案 5 :(得分:0)
我详细查看了你的Json String。只看你的&#34;图像&#34;键后跟一个JSONObject。
{
"current_observation": {
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
{
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
}
}
}
因为你的&#34;图像&#34; key有多个值,它应该在JSONArray中。所以你的String应该看起来像
{
"current_observation": {
"image": [{
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
{
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
}
]
}
}
我建议您再次检查字符串。