使用 java json 对象转换 将 attributesGZQYMC白庙村GZQYDM110115111216OLDGZQYDMgeometrypointsx11658689660034506y3968183559609521x11658690816565y3968112628238105x11658689660034506y3968183559609521x11658690816565y39
可以使用以下代码进行转换:
import org.json.JSONArray; import org.json.JSONObject;
public class JsonConversionExample { public static void main(String[] args) { String inputJson = "{"attributes":{"GZQYMC":"白庙村","GZQYDM":"110115111216","OLDGZQYDM":""},"geometry":{"points":[[{"x":116.58689660034506,"y":39.68183559609521},{"x":116.58690816565,"y":39.68112628238105}],[{"x":116.58689660034506,"y":39.68183559609521},{"x":116.58690816565,"y":39.68112628238105}]]}}";
// Convert inputJson to JSONObject
JSONObject inputJsonObject = new JSONObject(inputJson);
// Convert JSONObject "points" array to JSONArray
JSONArray pointsJsonArray = inputJsonObject.getJSONObject("geometry").getJSONArray("points");
// Loop through JSONArray and replace JSONObject "x" and "y" keys with double values
for (int i = 0; i < pointsJsonArray.length(); i++) {
JSONArray innerJsonArray = pointsJsonArray.getJSONArray(i);
for (int j = 0; j < innerJsonArray.length(); j++) {
JSONObject jsonObject = innerJsonArray.getJSONObject(j);
double x = jsonObject.getDouble("x");
double y = jsonObject.getDouble("y");
jsonObject.remove("x");
jsonObject.remove("y");
jsonObject.put("0", x);
jsonObject.put("1", y);
}
}
// Print the modified JSONObject
System.out.println(inputJsonObject.toString());
}
}
运行结果为:
{"attributes":{"GZQYMC":"白庙村","GZQYDM":"110115111216","OLDGZQYDM":""},"geometry":{"points":[[[116.58689660034506,39.68183559609521],[116.58690816565,39.68112628238105]],[[116.58689660034506,39.68183559609521],[116.58690816565,39.68112628238105]]]}
原文地址: https://www.cveoy.top/t/topic/fnVN 著作权归作者所有。请勿转载和采集!