Java 如何使用改型2.3.0解析json对象

Java 如何使用改型2.3.0解析json对象,java,android,json,retrofit2,Java,Android,Json,Retrofit2,我们的团队决定使用改型2,我正在对这个库做一些初步研究,但我是改型的新手。如何解析下面的Json { "main": { "totalResults": "500", "libelleCategory": "Véhilcule", "libelleSubCategory": "Mots et Vélos", "idCategory": "1", "idSubCategory": "3", "row": [ {

我们的团队决定使用改型2,我正在对这个库做一些初步研究,但我是改型的新手。如何解析下面的Json

    {
    "main": {
    "totalResults": "500",
    "libelleCategory": "Véhilcule",
    "libelleSubCategory": "Mots et Vélos",
    "idCategory": "1",
    "idSubCategory": "3",
    "row": [
       {
            "id": "66888",
            "shortURL": "https://www.testimage.com",
            "title": "Moto - HONDA - 2007",
            "text": "Pan lorem ipsum test test c'est un test",
            "img": "https://www.test.image.com",
           "price": "6 200",
           "datePublish": "05/05/2018",
           "nbPhotos": "3",
           "address": "75001 Paris"
       },
       {
           "id": "66889",
           "shortURL": "https://www.testimage.com",
           "title": "Moto  - 2018",
           "text": "Pan lorem ipsum test test c'est un test",
           "img": "https://www.test.image.com",
           "price": "9 500",
           "datePublish": "05/05/2018",
           "nbPhotos": "5",
           "address": "75001 Paris"
       }
     ]
  }
}

提前感谢

初始化API时,必须添加JSON转换器

我最喜欢的是杰克逊:

  • 添加依赖项:
    com.squareup.改装:转换器jackson

  • 将变矩器设置为改装

    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://your.domain")
    .addConverterFactory(JacksonConverterFactory.create()) // In real app, you should provide a preconfigured ObjectMapper for better performance
    .build();
    
  • 编辑

    例如:

    创建与JSON匹配的模型:

    public class YourResponse {
        private Detail main; // This will match "main": {}
    
        public static final class Detail {
           private String totalResults; // This will match "totalResults": ...
           private String libelleCategory;
           private String libelleSubCategory;
           ... bla bla....
           ... your getter/setter method....
        }
        ... your getter/setter method....
    }
    
    在API类中使用它:

    public interface SampleApi {
    
      @Get("/your/path/to/get")
      Call<YourResponse> getResponse();
    }
    
    调用您的API:

    SampleApi api = new Retrofit.Builder()
                        .baseUrl("https://your.domain")
                        .addConverterFactory(JacksonConverterFactory.create()) // In real app, you should provide a preconfigured ObjectMapper for better performance
                        .build()
                        .create(SampleApi.class);
    
    Response<YourResponse> serverResponse = api.getResponse().execute();
    if (serverResponse.isSuccessful()) {
      YourResponse = serverResponse.body();
    }
    
    Response serverResponse=api.getResponse().execute();
    if(serverResponse.issusccessful()){
    YourResponse=serverResponse.body();
    }
    
    您能提供更多详细信息吗?我应该把什么作为POJO?如果你能给我解析上面json的所有细节,那就太好了。谢谢你。这已经为你完成了。您不必做任何事情,在改装解析响应时将使用转换器查看上面的完整答案您需要为这个json()编写模型类,然后按照改装调用来解析它。