Java中将List集合数据转成带反斜杠的字符串格式
List<Map<String, Object>> trajectoryTrackList 得到数据:
[{latitude=32.01813267498781, coord_type_input=wgs84, loc_time=1702505122, longitude=118.69349822755345}, {latitude=32.01816666340177, coord_type_input=wgs84, loc_time=1702505255, longitude=118.6933972171819}, {latitude=32.01817768701138, coord_type_input=wgs84, loc_time=1702505673, longitude=118.69360023714842}, {latitude=32.018234689265356, coord_type_input=wgs84, loc_time=1702505771, longitude=118.69361823829318}, {latitude=32.0181096841538, coord_type_input=wgs84, loc_time=1702505940, longitude=118.69357723563404}, {latitude=32.018234691624556, coord_type_input=wgs84, loc_time=1702506149, longitude=118.6936382402779}, {latitude=32.01829170149445, coord_type_input=wgs84, loc_time=1702506402, longitude=118.69372024778653}, {latitude=32.01930782611061, coord_type_input=wgs84, loc_time=1702506497, longitude=118.69468533391556}, {latitude=32.02103302622464, coord_type_input=wgs84, loc_time=1702506565, longitude=118.69604245744496}, {latitude=32.022917179038195, coord_type_input=wgs84, loc_time=1702506630, longitude=118.69694053633664}]
转成类似这样的带反斜杠的字符串格式:
“[” +
“{“loc_time”:1624295452,“latitude”:36.2715924153,“longitude”:120.401133898,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624295615,“latitude”:36.2710674153,“longitude”:120.401122046,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624295715,“latitude”:36.2710674153,“longitude”:120.401122046,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624295815,“latitude”:36.26970681,“longitude”:120.406126,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624295915,“latitude”:36.2626899159,“longitude”:120.406257629,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624296015,“latitude”:36.2554959254,“longitude”:120.401709345,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624296115,“latitude”:36.2482072286,“longitude”:120.398668032,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624296215,“latitude”:36.2453202208,“longitude”:120.390573255,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624296315,“latitude”:36.2462943954,“longitude”:120.378697631,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624296415,“latitude”:36.2464401658,“longitude”:120.368128971,“coord_type_input”:“bd09ll”},” +
“{“loc_time”:1624296515,“latitude”:36.2411743783,“longitude”:120.360663512,“coord_type_input”:“bd09ll”}” +
“]”
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// 假设 trajectoryTrackList 是包含 Map<String, Object> 对象的列表数据
List<Map<String, Object>> trajectoryTrackList = new ArrayList<>();
// trajectoryTrackList 你的数据
String jsonString = convertListMapToJsonString(trajectoryTrackList);
System.out.println(jsonString);
}
private static String convertListMapToJsonString(List<Map<String, Object>> list) {
List<Map<String, Object>> convertedList = new ArrayList<>();
for (Map<String, Object> map : list) {
Map<String, Object> convertedMap = new HashMap<>();
convertedMap.put("loc_time", map.get("loc_time"));
convertedMap.put("latitude", map.get("latitude"));
convertedMap.put("longitude", map.get("longitude"));
convertedMap.put("coord_type_input", map.get("coord_type_input"));
convertedList.add(convertedMap);
}
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(convertedList);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!