Android 如何使用改型解析嵌套json。。。。?
我不知道如何使用改型解析json。我熟悉使用改型解析简单json,但不熟悉使用改型解析嵌套json 这是我的Json数据Android 如何使用改型解析嵌套json。。。。?,android,arrays,json,retrofit,Android,Arrays,Json,Retrofit,我不知道如何使用改型解析json。我熟悉使用改型解析简单json,但不熟悉使用改型解析嵌套json 这是我的Json数据 { "current_observation": { "image": { "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png", "title":"Weather
{
"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;
}
}
主活动中的代码
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);
}
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) {
}
});
Call Call=apinterface.getCountries();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.d(标记“onSuccess服务器响应”+Response.toString());
Log.d(标记“onSuccess-received-information”+response.body().toString());
列表项=response.body().getItems();
adapter=new RecAdapter(items,getContext().getApplicationContext());
recyclerView.setAdapter(适配器);
}
对于JSON解析,您应该使用convert JSON String to model class
在您的改装成功响应中
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;
}
}
模型类,如
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;
}
}
在
改装
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
}
如果正确实现Pojo,这将更容易
类和json中存在冲突
从您的国家/地区类别中,“当前观察”将是列表项;
那么您的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"
}
}]
在使用列表的情况下,请注意方括号中的[]
。即使只有一项,您的“当前观察”
仍需要将其声明为列表
我建议您使用以下网站:
选择源类型:JSON
,注释样式:Moshi
(我正在使用Moshi,或者您可以使用Gson),勾选:使类可序列化
它将为您的json生成正确的类。您的其余代码应该已经正确了
更新:
如果生成类后没有生成列表,则不应使用
List<Items> items = response.body().getItems();
在主要活动中如何称呼它
您需要像这样调用pojo类方法
String url=getCurrentObservation().getImage().getUrl();
如果你收到回复
String url=response.body().getCurrentObservation().getImage().getUrl();
有关改装的更多帮助,请参见my ansCurrentObservation.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 <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);
}
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) {
}
});
调用ex=BaseUrlClass.getInterface().ex(“任何参数”);
ex.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
示例列表=response.body();
CurrentObservation a=list.getCurrentObservation();
List im=a.getImage();
对于(inti=0;i我详细查看了Json字符串,只需查看“image”键后跟一个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"
}
}
}
因为“image”键有多个值,所以它应该在JSONArray中
{
"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对象,请在模型类中使用注释演示如何处理“简单JSON”,然后我们将建议如何修改。是的,我将展示它@Jacky@farhana是的,我知道。我的简单json代码将显示更多light@Jacky请浏览我的代码是的,谢谢兄弟……我怎么能在主活动中称之为……?你能给我看一些示例吗。@MilkaMozhi这是示例链接。希望它能帮助你展示整个Json,这是对象还是ar雷?等等,兄弟,我会把我所有的json都贴出来的bro@MilkaMozhi答案中有什么复杂的地方?sry bro我在看到你编辑的帖子之前收到了回复是的,你的正确的兄弟……名单……没有一个。我把它删掉了,以简化问题……我现在已经更新了。请过目it@MilkaMozhi这仍然是错误的,我的朋友,使用json验证器你可以看到。这就是为什么我怀疑你的json不能被序列化到你的类中。它给出了什么错误。你试过调试它吗?