我怎样才能解析一个使用Gson转换器在改造的关联数组? [英] How can I parse an associative array using Gson Converter in Retrofit?

查看:81
本文介绍了我怎样才能解析一个使用Gson转换器在改造的关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自PHP服务器的 JSON 响应。在android中,我需要编写一个Java模型(POJO)来解析Retrofit(一个用于http请求的Android库)中的响应。

JSON结构:

  {
calendar:{
2016-06-10:[
{
time:10h00m,
title:PROVA P2,
description:LP / RED / ED.FIS - 80 E 90,
color:#990000
}
],
2016-06-11:[
{
time:10h00m ,
title:SIMULADO,
description:LOREM PSIUM DOLOR LOREM,
color:#009900
},
{
time:11h00m,
title:CONSELHO DE CLASSE,
description:LOREM PSIUM DOLOR,
color: #009900
}
]
},
错误:false
}

这个 JSON 来自PHP服务器。
我如何使用改造来处理?

解决方案使用动态密钥解析 JSON ,您需要一个 Map 在您的 POJO 类中。

添加以下 POJO 类到您的项目:


  1. CalendarResponse .java

      public class CalendarResponse {
    @SerializedName(calendar)
    Map< String,List< Entry>>项;

    @SerializedName(error)
    私有布尔错误;


  2. Entry.java
    pre code $ public class Entry {
    @SerializedName(time)
    private String time;

    @SerializedName(title)
    私有字符串标题;

    @SerializedName(description)
    私有字符串描述;

    @SerializedName(color)
    私有字符串颜色;


  3. 使用 CalendarResponse b
    preublic $ CalendarService {
    @GET(

    pre $ <插入您自己的相对网址>)
    致电< CalendarResponse> listCalendar();

    code $ <$ $ p $


  4. 按以下方式执行调用(同步):

     调用< CalendarResponse> call = calendarService.listCalendar(); 
    CalendarResponse result = call.execute()。body();


如果需要, JSON GSON

  Gson gson = new GsonBuilder()。create(); 
CalendarResponse b = gson.fromJson(json,CalendarResponse.class);


I'm receiving a JSON response from a PHP server. In android, I need to write a java model (POJO) to use to parse the response in Retrofit (An Android library for http-requests).

JSON Structure:

{
  "calendar": {
    "2016-06-10": [
      {
        "time": "10h00m",
        "title": "PROVA P2",
        "description": "LP / RED / ED.FIS - 80 E 90",
        "color": "#990000"
      }
    ],
    "2016-06-11": [
      {
        "time": "10h00m",
        "title": "SIMULADO",
        "description": "LOREM PSIUM DOLOR LOREM",
        "color": "#009900"
      },
      {
        "time": "11h00m",
        "title": "CONSELHO DE CLASSE",
        "description": "LOREM PSIUM DOLOR",
        "color": "#009900"
      }
    ]
  },
  "error": false
}

This JSON is from PHP Server. How can I handle it using Retrofit?

解决方案

To parse JSON with dynamic keys, you will need a Map in your POJO class.

Add the following POJO classes to your project:

  1. CalendarResponse.java

    public class CalendarResponse {
      @SerializedName("calendar")
      Map<String, List<Entry>> entries;
    
      @SerializedName("error")
      private boolean error;
    }
    

  2. Entry.java

    public class Entry {
      @SerializedName("time")
      private String time;
    
      @SerializedName("title")
      private String title;
    
      @SerializedName("description")
      private String description;
    
      @SerializedName("color")
      private String color;
    }
    

  3. Use the CalendarResponse class in your retrofit interface for your endpoint, see example below

    public interface CalendarService {
      @GET("<insert your own relative url>")
      Call<CalendarResponse> listCalendar();
    }
    

  4. Execute the call (synchronously) as follows:

    Call<CalendarResponse> call = calendarService.listCalendar();
    CalendarResponse result = call.execute().body();
    

If needed, here is an example to parse the JSON with GSON:

Gson gson = new GsonBuilder().create();
CalendarResponse b = gson.fromJson(json, CalendarResponse.class);

这篇关于我怎样才能解析一个使用Gson转换器在改造的关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆