英文:
Formatting Json Response into an Array Java
问题
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
Currently I have tried:
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.
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.
英文:
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
Currently I have tried:
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.
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.
答案1
得分: 1
你可以使用ObjectMapper将json
字符串转换为某个对象,在这种情况下,类似于List<Map<String,Object>>
。然后使用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]
总的来说,你可以编写自己的扁平化算法。例如:
英文:
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:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.1</version>
</dependency>
Read json
string value:
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);
Then iterate over this list:
List<Object> flatList = list.stream()
.flatMap(element -> element.values().stream())
.collect(Collectors.toList());
System.out.println(flatList); // [1, apartment, 2, 2, lodge, 5]
Or more detailed variant:
List<Object> flatList = list.stream()
.map(Map::values)
.collect(Collectors.toList());
System.out.println(flatList); // [[1, apartment, 2], [2, lodge, 5]]
And more:
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:
答案2
得分: 0
你可以创建一个POJO类,然后使用类似Jackson或Gson的库将JSON字符串映射到POJO实例数组中。在这种情况下,我将使用Jackson,你可以通过Maven导入它:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.0</version>
</dependency>
POJO类。请注意,我使用@JsonProperty
注解来设置JSON字段名称,以便避免使用包含特殊字符的变量名称:
import com.fasterxml.jackson.annotation.JsonProperty;
public class APIResponse {
@JsonProperty("$id")
private int id;
@JsonProperty("accommodation_type")
private String accommodationType;
@JsonProperty("max_people")
private int maxPeople;
public int getId() {
return id;
}
public int getMaxPeople() {
return maxPeople;
}
public String getAccommodationType() {
return accommodationType;
}
@Override
public String toString() {
return "APIResponse{" +
"id=" + id +
", accommodationType='" + accommodationType + '\'' +
", maxPeople=" + maxPeople +
'}';
}
}
然后,你可以使用以下方式进行反序列化:
final String json = "[{\"$id\":\"1\",\"accommodation_type\":\"apartment\",\"max_people\":2},{\"$id\":\"2\",\"accommodation_type\":\"lodge\",\"max_people\":5}]";
final ObjectMapper mapper = new ObjectMapper();
APIResponse[] responses = mapper.readValue(json, APIResponse[].class);
for (APIResponse response: responses) {
System.out.println(response.toString());
}
结果:
APIResponse{id=1, accommodationType='apartment', maxPeople=2}
APIResponse{id=2, accommodationType='lodge', maxPeople=5}
最后,你可以通过调用POJO类中的getter方法来访问数据:
responses[0].getId(); // 1
responses[1].getAccommodationType(); // lodge
然后,如果你想要以逗号分隔的方式获取数据,可以使用以下方法:
public String[] getByCommas(APIResponse[] responses) {
List<String> data = new ArrayList<>();
for (APIResponse response: responses) {
data.add("id,");
data.add(response.getId() + ",");
data.add("accommodation_type,");
data.add(response.getAccommodationType() + ",");
data.add("max_people,");
data.add(response.getMaxPeople() + ",");
}
return data.toArray(new String[data.size()]);
}
然后,只需使用以下方式:
String[] formattedMessage = getByCommas(responses);
for (String s: formattedMessage) {
System.out.print(s);
}
结果:
id,1,accommodation_type,apartment,max_people,2,id,2,accommodation_type,lodge,max_people,5,
使用JSON映射器在解析JSON数据时非常可靠。如果有其他问题,请告诉我!
英文:
You can create a POJO class and then use libraries like Jackson or Gson to map the JSON string into an array of POJO instances. In this case I will use Jackson you can import it via maven with:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.0</version>
</dependency>
The POJO class. Note that I use the annotation @JsonProperty
to set the JSON field names so I can avoid using variable names that contain special characters.
import com.fasterxml.jackson.annotation.JsonProperty;
public class APIResponse {
@JsonProperty("$id")
private int id;
@JsonProperty("accommodation_type")
private String accommodationType;
@JsonProperty("max_people")
private int maxPeople;
public int getId() {
return id;
}
public int getMaxPeople() {
return maxPeople;
}
public String getAccommodationType() {
return accommodationType;
}
@Override
public String toString() {
return "APIResponse{" +
"id=" + id +
", accommodationType='" + accommodationType + '\'' +
", maxPeople=" + maxPeople +
'}';
}
}
Then you can deserialize using:
final String json = "[{\"$id\":\"1\",\"accommodation_type\":\"apartment\",\"max_people\":2},{\"$id\":\"2\",\"accommodation_type\":\"lodge\",\"max_people\":5}]";
final ObjectMapper mapper = new ObjectMapper();
APIResponse[] responses = mapper.readValue(json, APIResponse[].class);
for (APIResponse response: responses) {
System.out.println(response.toString());
}
Result:
APIResponse{id=1, accommodationType='apartment', maxPeople=2}
APIResponse{id=2, accommodationType='lodge', maxPeople=5}
Finally, you can access the data just by calling the getters in the POJO class:
responses[0].getId(); // 1
responses[1].getAccommodationType; // lodge
Then if you want the data separated by commas use:
public String[] getByComas(APIResponse[] responses) {
List<String> data = new ArrayList<>();
for (APIResponse response: responses) {
data.add("id,");
data.add(response.getId() + ",");
data.add("accommodation_type,");
data.add(response.getAccommodationType() + ",");
data.add("max_people,");
data.add(response.getMaxPeople() + ",");
}
return data.toArray(new String[data.size()]);
}
Then just use:
String[] formattedMessage = getByComas(responses);
for (String s: formattedMessage) {
System.out.print(s);
}
Result:
id,1,accommodation_type,apartment,max_people,2,id,2,accommodation_type,lodge,max_people,5,
Using JSON mappers is highly recommended as they are pretty reliable when parsing JSON data.
Let me know if this solves your problem!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论