我怎样才能解析一个使用Gson转换器在改造的关联数组? [英] How can I parse an associative array using Gson Converter in Retrofit?
问题描述
我收到来自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
类到您的项目:
-
CalendarResponse .java
public class CalendarResponse {
@SerializedName(calendar)
Map< String,List< Entry>>项;
@SerializedName(error)
私有布尔错误;
-
Entry.java $ c
pre code $ public class Entry {
@SerializedName(time)
private String time;
@SerializedName(title)
私有字符串标题;
@SerializedName(description)
私有字符串描述;
@SerializedName(color)
私有字符串颜色;
-
使用
CalendarResponse $ c $ b> b
preublic $ CalendarService {
@GET(
pre $ <插入您自己的相对网址>)
致电< CalendarResponse> listCalendar();
code $ <$ $ p $ -
按以下方式执行调用(同步):
调用< 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:
CalendarResponse.java
public class CalendarResponse { @SerializedName("calendar") Map<String, List<Entry>> entries; @SerializedName("error") private boolean error; }
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; }
Use the
CalendarResponse
class in your retrofit interface for your endpoint, see example belowpublic interface CalendarService { @GET("<insert your own relative url>") Call<CalendarResponse> listCalendar(); }
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屋!