我有以下JSON字符串:
{
"ms": "images,5160.1",
"turl": "http://ts1.mm.bing.net/th?id=I4693880201938488&pid=1.1",
"height": "178",
"width": "300",
"imgurl": "http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg",
"offset": "0",
"t": "World Cup 2014 Qualification – Europe Draw World Cup 2012 Draw ...",
"w": "719",
"h": "427",
"ff": "jpeg",
"fs": "52",
"durl": "www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe...",
"surl": "http://www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe-draw/world-cup-2012-draw/",
"mid": "D9E91A0BA6F9E4C65C82452E2A5604BAC8744F1B",
"k": "6",
"ns": "API.images"
}
Run Code Online (Sandbox Code Playgroud)
我需要将值存储imgurl在单独的字符串中.
这就是我现在所拥有的,但这只是给了我整个JSON字符串而不是特定的imgurl字段.
Gson gson = new Gson();
Data data = new Data();
data = gson.fromJson(toExtract, Data.class);
System.out.println(data);
Run Code Online (Sandbox Code Playgroud)
toExtract是JSON字符串.这是我的数据类:
public class Data
{
public List<urlString> myurls;
}
class urlString
{
String imgurl; …Run Code Online (Sandbox Code Playgroud) 我们的团队决定使用Retrofit 2.0,我正在对这个库进行一些初步研究.如标题中所述,我想通过我们的Android应用程序中的Retrofit 2.0解析一些嵌套的JSON对象.
例如,这是一个嵌套的JSON对象,格式为:
{
"title": "Recent Uploads tagged android",
"link": "https://www.flickr.com/photos/tags/android/",
"description": "",
"modified": "2015-10-05T05:30:01Z",
"generator": "https://www.flickr.com/",
"items": [
{
"title": ...
"link": ...
"media": {"m":"This is the value I want to get:)"}
"description": ...
"published": ...
"author": ...
"author_id": ...
"tags": ...
},
{...},
...
]
}
Run Code Online (Sandbox Code Playgroud)
我对items数组中的JSON对象感兴趣.我注意到有一些关于通过Retrofit 1.X解析嵌套JSON对象的帖子,但是最新的Retrofit 2.0 API已经发生了很大变化,这在将它们适应新API时会让人感到困惑.
我想到了两种可能的解决方案:
StringConverter.(我可能错了〜)更新:我们实际上可以通过设置JSONElement为HTTP API接口的pojo并使用Retrofit提供的GSONConverter作为转换器来获取原始响应.
我试图从JSON响应中反序列化对象列表.JSON数组有一个密钥,这导致使用GSON对其进行反序列化的问题.
我有大约20个与此类似的对象.
public class Device extends Entity {
String device_id;
String device_type;
String device_push_id;
}
Run Code Online (Sandbox Code Playgroud)
对于大多数人来说,有一种API方法可以返回一个对象列表.返回的JSON看起来像这样.由于其他客户端,此时更改JSON的格式不是一个合理的选择.
{
"devices":[
{
"id":"Y3mK5Kvy",
"device_id":"did_e3be5",
"device_type":"ios"
},
{
"id":"6ZvpDPvX",
"device_id":"did_84fdd",
"device_type":"android"
}
]
}
Run Code Online (Sandbox Code Playgroud)
为了解析这种类型的响应,我目前正在使用混合org.json方法和Gson.
JSONArray jsonResponse = new JSONObject(response).getJSONArray("devices");
Type deviceListType = new TypeToken<List<Device>>() {}.getType();
ArrayList<Device> devices = gson.fromJson(jsonResponse.toString(), deviceListType);
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种更干净的反序列化方法,因为我想使用Retrofit.使用改进使用GSON获取嵌套JSON对象的答案接近我的需要,但不处理Lists.我在这里复制了答案的通用版本:
public class RestDeserializer<T> implements JsonDeserializer<T> {
private Class<T> mClass;
private String mKey;
public RestDeserializer(Class<T> targetClass, String key) {
mClass = targetClass;
mKey = key;
}
@Override …Run Code Online (Sandbox Code Playgroud) 我正在使用一个始终返回如下所示的JSON对象的API:
public class ApiResponse<T> {
public boolean success;
public T data;
}
Run Code Online (Sandbox Code Playgroud)
data field是一个JSON对象,包含所有有价值的信息.当然,对于不同的请求,它们是不同的.所以我的改造界面如下所示:
@GET(...)
Observable<ApiResponse<User>> getUser();
Run Code Online (Sandbox Code Playgroud)
当我想处理响应时我需要做的事如:
response.getData().getUserId();
Run Code Online (Sandbox Code Playgroud)
我真的不需要那个布尔成功字段,我想省略它,所以我的改装界面可能如下所示:
@GET(...)
Observable<User> getUser();
Run Code Online (Sandbox Code Playgroud)
在Gson可以这样做吗?或者可能是一个整洁的Rx功能,它会自动转换它?
编辑:示例json:
{
"success": true,
"data": {
"id": 22,
"firstname": "Jon",
"lastname": "Snow"
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2:我已经通过改装拦截器来手动修改响应体.它有效,但如果您有任何其他建议,请发布它们:)