使用Moshi转换JSON对象时,'-'这个符号怎么转化

image.png
下面是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;
  }

image.png
在Gist里面定义一个html_url是可以获取到返回值的

但是
如图
image.png
这个里面有“-”符号,一般是用的下划线连接,但是如果用“-”连接在IDE里面命名会报错,请问这个应该用什么符号转换呢。

阅读 2.6k
1 个回答

@Json注解

@Json(name = "not-ready-reason")
private Integer notReadyReason;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题