将Json响应格式化为数组Java [英] Formatting Json Response into an Array Java

查看:116
本文介绍了将Json响应格式化为数组Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我从我的API收到这样的响应:

Currently I am receiving a response like this from my API:

[{"$id":"1","accommodation_type":"apartment","max_people":2},{"$id":"2","accommodation_type":"lodge","max_people":5}]

我想对它进行格式化,以便输出删除所有不必要的标点,使它看起来更像这样,同时也将其放入数组中。

I would like to format it so that the output removes all the unnecessary punctuation so that it looks more like this whilst also placing it into an Array.

id, 1, accommodation_type, apartment, max_people, 2, id, 2, accommodation_type, lodge, max_people 5

OR

1, apartment, 2, ,2, lodge, 5

当前我已经尝试过:

String temp[]= AccommodationTypesStr.split(":|\\,|\\}"); // Where AccommodationTypesStr is the input json string

但是,每行数据之间在元素中都留有空白数组,如下所示:

However between each row of data it leaves a empty space as a element in the array so its like:

id, 1, accomodation_type, apartment, max_people, 2,  ,id, 2, accommodation_type, lodge, max_people 5

虽然响应中仍然包含一些括号。

Whilst also still having some brackets in the response.

我已经弄乱了JSON对象和数组,但是一点都不走运,所以想知道是否可以自己格式化它。

I've messed around with JSON Object and Array but had no luck at all so was wondering if I could do it by formatting it myself.

推荐答案

您可以使用 ObjectMapper json 字符串转换为某个对象,在这种情况下,例如 List< Map< String,Object>> 。然后使用 java流api 遍历此列表。

You can use ObjectMapper to convert json string to some object, in this case like List<Map<String, Object>>. Then iterate over this list using java stream api.

Maven依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.11.1</version>
</dependency>

读取 json 字符串值:

String json = "[{\"$id\":\"1\",\"accommodation_type\":\"apartment\",\"max_people\":2},{\"$id\":\"2\",\"accommodation_type\":\"lodge\",\"max_people\":5}]";
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> list = mapper.readValue(json, List.class);

然后遍历此列表:

List<Object> flatList = list.stream()
    .flatMap(element -> element.values().stream())
    .collect(Collectors.toList());

System.out.println(flatList); // [1, apartment, 2, 2, lodge, 5]

或更详细的变体:

List<Object> flatList = list.stream()
    .map(Map::values)
    .collect(Collectors.toList());

System.out.println(flatList); // [[1, apartment, 2], [2, lodge, 5]]

以及更多:

List<Object> flatList = list.stream()
    .flatMap(element -> element.entrySet().stream())
    .flatMap(entry -> Stream.of(
        entry.getKey().replace("$", ""), // without "$"
        entry.getValue()))
    .collect(Collectors.toList());

System.out.println(flatList);
// [id, 1, accommodation_type, apartment, max_people, 2, id, 2, accommodation_type, lodge, max_people, 5]




通常,您可以编写自己的展平算法。例如:


In general, you can write your own flattening algorithm. For example:

  • «Flatten nested Map containing with unknown level of nested Arrays and Maps recursively».

«从其平面图表示形式还原值树»

这篇关于将Json响应格式化为数组Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆