java - 使用Retrofit进行JSON解析

标签 java json parsing retrofit

我有一个示例 JSON 文件,我想使用 Retrofit 在 Java 中解析该文件。我对 Retrofit 很陌生,对 Java 也有点陌生。我在网上看到的例子不是 我现在就清楚了。有人可以解释一下如何使用 Retrofit 从以下 JSON 结构中提取 movie_logo 字段吗?

  "url":"sample_url",
  "movies_metadata":
  {
    "movies":
    {
       "Movie 1":
        {
          "Description":"Sample description for Movie 1",
           "Movie_Logo":"logo1.png"
        },
        "Movie 2":
        {
           "Description":"Sample description for Movie 2",
           "Movie_Logo":"logo1.png"
        },
       "Movie 3":
        {
           "Description":"Sample description for Movie 3",
           "Movie_Logo":"logo1.png"
        }
      }
   }

最佳答案

Retrofit 并不是真正用于将 JSON 解析为 Java 对象(在内部它实际上使用 GSON 来解析 API 响应)。我建议使用JSON.org , GSONJackson用于解析您的 JSON 文件。最简单的方法是使用 JSON.org 解析器:

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;

@Slf4j
public class JsonTest {
    @Test
    public void test() throws Exception {
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("http://jsonblob.com/api/jsonBlob/5384f843e4b0441b35d1329d");
        request.addHeader("accept", "application/json");
        HttpResponse response = client.execute(request);
        String json = IOUtils.toString(response.getEntity().getContent());

        //here's where you're actually parsing the JSON
        JSONObject object = new JSONObject(json);
        JSONObject metadata = object.getJSONObject("movies_metadata");
        JSONObject movies = metadata.getJSONObject("movies");
        JSONArray movieNames = movies.names();
        for (int i = 1; i< movieNames.length(); i++) {
            String movieKey = movieNames.getString(i);
            log.info("The current object's key is {}", movieKey);
            JSONObject movie = movies.getJSONObject(movieKey);
            log.info("The Description is {}", movie.getString("Description"));
            log.info("The Movie_Logo is {}", movie.getString("Movie_Logo"));
        }
    }
}

我将你的 JSON 放入 JSON Blob然后使用他们的 API 在单元测试中请求它。单元测试的输出是:

14:49:30.450 [main] INFO  JsonTest - The current object's key is Movie 2
14:49:30.452 [main] INFO  JsonTest - The Description is Sample description for Movie 2
14:49:30.452 [main] INFO  JsonTest - The Movie_Logo is logo1.png
14:49:30.452 [main] INFO  JsonTest - The current object's key is Movie 1
14:49:30.453 [main] INFO  JsonTest - The Description is Sample description for Movie 1
14:49:30.453 [main] INFO  JsonTest - The Movie_Logo is logo1.png

关于java - 使用Retrofit进行JSON解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23898266/

相关文章:

json - 如何在 Golang 结构中使用 omitempty 标志更新 Mongodb 字段

bash - 获取某个单词后的子字符串

ios - Swift 代码中的 EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)。为什么?

java - 云端点方法可见性

json - Angular 2 HTTP post方法不会将数据发送到request.JSON在Grails Action中

java - String.compareTo方法进行比较

javascript - 无法执行ajax post操作

python - 使用 Python 解析 JSON 文件中的数据

java - 滚动 ListView 时 TextView 中的数据切换

java - 将多个键添加到 hashMap 中的值