一、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());
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/290827.html