Java 在Spring中解析GET JSON reply而不使用对象的键

Java 在Spring中解析GET JSON reply而不使用对象的键,java,json,spring,rest,Java,Json,Spring,Rest,我目前正在以 "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"}]},{},{},{},{},{},{},{

我目前正在以

"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
显然,这不是解决问题的办法,因为我应该能够以某种方式自动解析它。我也只需要第二个元素的内容(数组,从
potterharry
potterfeniks

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

@Test
@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());});
}
public void testJson(){ RestTemplate=新的RestTemplate(); ResponseEntity=模板。 交换(”https://api.myjson.com/bins/2rl7m,HttpMethod.GET,null,新参数化类型引用(){ }); ArrayNode body=entity.getBody(); body.get(1.forEach)(m->{ System.out.println(m.asText();}); }

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

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

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;
}

}
public类JSONHelper{
私有静态最终JsonFactory=新JsonFactory();
私有静态最终ObjectMapper mapper=新ObjectMapper(工厂);
公共静态列表getListOnPosition(int i,String inputWithFullList)抛出JsonProcessingException、IOException{
列表结果=新建ArrayList();
JsonNode rootNode=mapper.readTree(inputWithFullList);
ArrayNode节点=(ArrayNode)rootNode.get(i);
如果(!node.isArray()){
add(node.asText());
}否则{
for(最终JsonNode子节点:node){
add(subNode.asText());
}
}
返回结果;
}
}
此场景的一些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());
}
}
公共类JSONHelperTest{
@试验
public void parseListOnPositionFullListTest()抛出JsonProcessingException,IOException{
字符串inputWithFullList=“[\“a\”、[\“b\”、“c\”、“d\”、[]、[]”;
List result=JSONHelper.getListOnPosition(1,inputWithFullList);
assertEquals(3,result.size());
assertEquals(Arrays.asList(“b”、“c”、“d”)、result);
}
@试验
public void parseListonPositionElementListTest()抛出JsonProcessingException,IOException{
字符串inputWithFullList=“[\“a\”,[\“b\”],[],[],[]”;
List result=JSONHelper.getListOnPosition(1,inputWithFullList);
assertEquals(1,result.size());
assertEquals(Arrays.asList(“b”),结果);
}
@试验
public void parseListOnPositionEmptyListTest()抛出JsonProcessingException,IOException{
字符串inputWithFullList=“[\”a\”、[]、[]、[]、[]、[]”;
assertTrue(JSONHelper.getListOnPosition(1,inputWithFullList.isEmpty());
}
}

首先,消费内容的JSON格式不好,在rest模板中,您可以传递所需的对象,如果spring rest模板具有合适的消息转换器,它将“自动”解析响应。例如:
ResponseEntity=getTemplate().exchange(builder.toUri(),HttpMethod.GET,null,新参数化类型引用(){})好吧,我如何为这样一个没有键的响应创建一个合适的消息转换器?你到底没有得到什么?这个到amazon的url,你帮我回答了吗?