一、jsonArray转数组
1、使用org.json.JSONArray对象来创建JSONArray
JSONArray jsonArray = new JSONArray("[\"a\", \"b\", \"c\"]");
String[] stringArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
stringArray[i] = jsonArray.getString(i);
}
2、使用Gson库把JSONArray转成String数组
JSONArray jsonArray = new JSONArray("[\"a\", \"b\", \"c\"]");
Gson gson = new Gson();
String[] stringArray = gson.fromJson(jsonArray.toString(), String[].class);
3、使用Jackson库把JSONArray转成String数组
JSONArray jsonArray = new JSONArray("[\"a\", \"b\", \"c\"]");
ObjectMapper objectMapper = new ObjectMapper();
String[] stringArray = objectMapper.readValue(jsonArray.toString(), String[].class);
二、jsonarray转对象数组
1、使用org.json.JSONArray对象来创建JSONArray
JSONArray jsonArray = new JSONArray("[{\"name\":\"alice\",\"age\":23},{\"name\":\"bob\",\"age\":25}]");
Gson gson = new Gson();
Person[] persons = new Person[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
persons[i] = gson.fromJson(jsonArray.get(i).toString(), Person.class);
}
2、使用Jackson库把JSONArray转成对象数组
JSONArray jsonArray = new JSONArray("[{\"name\":\"alice\",\"age\":23},{\"name\":\"bob\",\"age\":25}]");
ObjectMapper objectMapper = new ObjectMapper();
Person[] persons = objectMapper.readValue(jsonArray.toString(), Person[].class);
三、jsonarray转成数组
1、使用org.json.JSONArray对象来创建JSONArray
JSONArray jsonArray = new JSONArray("[1, 2, 3]");
int[] intArray = new int[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
intArray[i] = jsonArray.getInt(i);
}
2、使用Gson库把JSONArray转成int数组
JSONArray jsonArray = new JSONArray("[1, 2, 3]");
Gson gson = new Gson();
int[] intArray = gson.fromJson(jsonArray.toString(), int[].class);
3、使用Jackson库把JSONArray转成int数组
JSONArray jsonArray = new JSONArray("[1, 2, 3]");
ObjectMapper objectMapper = new ObjectMapper();
int[] intArray = objectMapper.readValue(jsonArray.toString(), int[].class);
四、jsonarray转换成string
1、使用org.json.JSONArray对象来创建JSONArray
JSONArray jsonArray = new JSONArray("[\"a\", \"b\", \"c\"]");
String jsonString = jsonArray.toString();
2、使用Gson库把JSONArray转成String
JSONArray jsonArray = new JSONArray("[\"a\", \"b\", \"c\"]");
Gson gson = new Gson();
String jsonString = gson.toJson(jsonArray);
3、使用Jackson库把JSONArray转成String
JSONArray jsonArray = new JSONArray("[\"a\", \"b\", \"c\"]");
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(jsonArray);
五、jsonarray string
1、使用org.json.JSONArray对象来创建JSONArray
JSONArray jsonArray = new JSONArray("[\"a\", \"b\", \"c\"]");
String jsonArrayString = jsonArray.toString();
2、使用Gson库把String转成JSONArray
String jsonArrayString = "[\"a\", \"b\", \"c\"]";
Gson gson = new Gson();
JSONArray jsonArray = new JSONArray(gson.fromJson(jsonArrayString, List.class));
3、使用Jackson库把String转成JSONArray
String jsonArrayString = "[\"a\", \"b\", \"c\"]";
ObjectMapper objectMapper = new ObjectMapper();
JSONArray jsonArray = new JSONArray(objectMapper.readValue(jsonArrayString, List.class));
六、string转jsonarray对象
1、使用org.json.JSONArray对象来创建JSONArray
String jsonArrayString = "[\"a\", \"b\", \"c\"]";
JSONArray jsonArray = new JSONArray(jsonArrayString);
2、使用Gson库把String转成JSONArray
String jsonArrayString = "[\"a\", \"b\", \"c\"]";
Gson gson = new Gson();
JsonElement jsonElement = gson.fromJson(jsonArrayString, JsonElement.class);
JSONArray jsonArray = new JSONArray(jsonElement.toString());
3、使用Jackson库把String转成JSONArray
String jsonArrayString = "[\"a\", \"b\", \"c\"]";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonArrayString);
JSONArray jsonArray = new JSONArray(jsonNode.toString());