下面是okHttp获取JSON返回的一般实例,我在Gist里面添加了一个html_url字段
private final OkHttpClient client \= new OkHttpClient();
private final Moshi moshi \= new Moshi.Builder().build();
private final JsonAdapter<Gist\> gistJsonAdapter \= moshi.adapter(Gist.class);
public void run() throws Exception {
Request request \= new Request.Builder()
.url("https://api.github.com/gists/c2a7c39532239ff261be")
.build();
try (Response response \= client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Gist gist \= gistJsonAdapter.fromJson(response.body().source());
for (Map.Entry<String, GistFile\> entry : gist.files.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue().content);
}
}
}
static class Gist {
Map<String, GistFile\> files;
Strnig html_url;
}
static class GistFile {
String content;
}
在Gist里面定义一个html_url是可以获取到返回值的
但是
如图
这个里面有“-”符号,一般是用的下划线连接,但是如果用“-”连接在IDE里面命名会报错,请问这个应该用什么符号转换呢。
用
@Json
注解