【问题标题】:Parsing GET JSON reply without keys to object in Spring在 Spring 中解析没有对象键的 GET JSON 回复
【发布时间】:2015-07-23 05:27:40
【问题描述】:

我目前正在消费风格的内容

"potter",["potter harry","potter","potter hallows 2","potter collection","potter hallows 1","potter dvd box","potter 7","potter hallows","potter blue ray","potter feniks"],[{"xcats":[{"name":"dvd_all"},{"name":"books_nl"}]},{},{},{},{},{},{},{},{},{}],[]]

在 Spring 中使用以下代码

RestTemplate restTemplate = new RestTemplate();
String information = restTemplate.getForObject(URL, String.class);
//further parsing of information, using String utilities

显然这不是要走的路,因为我应该能够以某种方式自动解析它。我也只需要第二个元素的内容(数组,从potter harrypotter feniks)。

当它的 json 内容不是名称值时,解析这样的 GET 响应的最佳方法是什么?

【问题讨论】:

  • 首先,消费内容的 JSON 格式也不是很好,在 rest 模板中你可以传递你想要的对象,如果 spring rest 模板有合适的消息转换器,它将“自动”解析响应。例如:ResponseEntity<Map<String,AdInsightDataMixin>> entity = getTemplate().exchange(builder.toUri(), HttpMethod.GET, null, new ParameterizedTypeReference<Map<String, AdInsightDataMixin>>() { });
  • 那么,我如何为这样的响应创建一个合适的消息转换器,它没有键?

标签: java json spring rest


【解决方案1】:
@Test
public  void testJson(){
    RestTemplate template = new RestTemplate();
    ResponseEntity<ArrayNode> entity = template.
            exchange("https://api.myjson.com/bins/2rl7m", HttpMethod.GET, null, new ParameterizedTypeReference<ArrayNode>() {
            });

    ArrayNode body = entity.getBody();
    body.get(1).forEach(m->{
        System.out.println(m.asText());});
}

但我的建议是,如果您可以将响应类型更改为不包含混合值类型的 json 数组会更好

【讨论】:

  • 你到底没有得到什么?
  • 这个网址到亚马逊,你帮我回答了吗?
【解决方案2】:

以下帮助类使我能够轻松解析响应并获取所需的列表

public class JSONHelper {

private static final JsonFactory factory = new JsonFactory();

private static final ObjectMapper mapper = new ObjectMapper(factory);

public static List<String> getListOnPosition(int i, String inputWithFullList) throws JsonProcessingException, IOException {
    List<String> result = new ArrayList<String>();
    JsonNode rootNode = mapper.readTree(inputWithFullList);
    ArrayNode node = (ArrayNode) rootNode.get(i);
    if (!node.isArray()) {
        result.add(node.asText());
    } else {
        for (final JsonNode subNode : node) {
            result.add(subNode.asText());
        }
    }
    return result;
}

}

针对此场景的一些 JUnit 测试

public class JSONHelperTest {
@Test
public void parseListOnPositionFullListTest() throws JsonProcessingException, IOException {
    String inputWithFullList = "[\"a\",[\"b\", \"c\", \"d\"],[],[]]";
    List<String> result = JSONHelper.getListOnPosition(1, inputWithFullList);
    assertEquals(3, result.size());
    assertEquals(Arrays.asList("b", "c", "d"), result);
}

@Test
public void parseListOnPositionOneElementListTest() throws JsonProcessingException, IOException {
    String inputWithFullList = "[\"a\",[\"b\"],[],[]]";
    List<String> result = JSONHelper.getListOnPosition(1, inputWithFullList);
    assertEquals(1, result.size());
    assertEquals(Arrays.asList("b"), result);
}

@Test
public void parseListOnPositionEmptyListTest()  throws JsonProcessingException, IOException {
    String inputWithFullList = "[\"a\",[],[],[]]";
    assertTrue(JSONHelper.getListOnPosition(1, inputWithFullList).isEmpty());
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多