Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JSON解析与改进_Android_Json_Retrofit - Fatal编程技术网

Android JSON解析与改进

Android JSON解析与改进,android,json,retrofit,Android,Json,Retrofit,我最近开始改型。我对它了解不多。我在谷歌上搜索了这个问题,没有找到适合我问题的答案 这是JSON响应 { "results": [ { "description_eng": "This is second time testing", "img_url": "-", "title_eng": "Second test" }, { "description_eng": "Hello 1 2 3, I am testing.

我最近开始改型。我对它了解不多。我在谷歌上搜索了这个问题,没有找到适合我问题的答案

这是JSON响应

{
  "results": [
    {
      "description_eng": "This is second time testing",
      "img_url": "-",
      "title_eng": "Second test"
    },
    {
      "description_eng": "Hello 1 2 3, I am testing.",
      "img_url": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/s720x720/10838273_816509855058935_6428556113200361121_o.jpg",
      "title_eng": "Test"
    }
  ]
}
这是饲料课

public class Feed {
    public List<Results> results;
    class Results{
        String description_eng,img_url,title_eng;
    }
}
公共类提要{
公开名单结果;
课堂成绩{
字符串描述(英文),img(网址),title(英文);;
}
}
这是接口

public interface GetApi {
    @GET("/api.json")
    public void getData(Callback<List<Feed>> response);
}
公共接口GetApi{ @获取(“/api.json”) public void getData(回调响应); }
我得到了
json\u非法的\u语法异常

改型默认使用Gson将HTTP主体转换为json和从json转换过来。如果要指定不同于Gson默认值的行为(例如命名策略、日期格式、自定义类型),请在构建RestaAdapter时提供一个新的Gson实例,其中包含所需的行为

Gson无法自动反序列化纯内部类,因为它们的no-args构造函数还需要对包含对象的引用,而该对象在反序列化时不可用。您可以通过使内部类成为静态或为其提供自定义InstanceCreator来解决此问题。以下是一个例子:

public class A { 
  public String a; 

  class B { 

    public String b; 

    public B() {
      // No args constructor for B
    }
  } 
}
注意:上述B类不能(默认情况下)用Gson序列化


您应该阅读更多关于

这就是我如何通过创建空构造函数来解决这个问题的

Feed.class

public class Feed{
    private List<Result> results;

    public Feed(){}

    public List<Result> getFeed(){
        return this.results;
    }

    public void setFeed(List<Result> results) {
        this.results = results;
    }
}
GetApi.class

public interface GetApi {
    @GET("/api.json")
    public void getData(Callback<Feed> response);
}
public interface GetApi {
@GET("/api.json")
    public void getData(Callback<FeedList<Result>> response);
} 
公共接口GetApi{ @获取(“/api.json”) public void getData(回调响应); }
@yeminhtut实际上,更好的方法是使用泛型编写提要类

public class FeedList<T>{
   private List<T> feeds;
   public Feed() {

   }
   public List<T> getFeed(){
       return this.feeds;
   }
   public void setFeed(List<T> results) {
       this.feeds = feeds;
   }
}
公共类提要列表{
私有列表提要;
公共提要(){
}
公共列表getFeed(){
把这个还给我;
}
公共void setFeed(列出结果){
this.feed=feed;
}
}
如果只有一个对象存在类似的情况,则可以删除列表部分并获取/设置单个对象

现在,您可以调用
FeedList
或使用任何您想要的对象

GetApi.class

public interface GetApi {
    @GET("/api.json")
    public void getData(Callback<Feed> response);
}
public interface GetApi {
@GET("/api.json")
    public void getData(Callback<FeedList<Result>> response);
} 
公共接口GetApi{ @获取(“/api.json”) public void getData(回调响应); }
@Ye-Min-Htut我建议您使用Generator创建模型/POJO类。它将为您生成所有模型类,您不需要自己创建,将来在创建模型类时也将帮助您。它只需要JSON字符串,单击将使您的工作完成

尝试使“Results”类成为静态的,因为您已经将其设计为一个内部类。好的。您是否尝试用类似的方式解析响应JSON?如果它是一个“非法语法”错误,它应该告诉你。我刚刚测试过它。它是有效的。也许你应该为classNope bro提供一个空构造函数。已经测试过了。不工作。谢谢你的建议兄弟们:)我有一个类似的问题: