java解析json数组字符串
时间: 2023-08-05 16:32:51 浏览: 121
在Java中解析JSON数组字符串可以使用Java自带的org.json库或者第三方库如Jackson、Gson等。
使用org.json库解析JSON数组字符串的示例代码:
```java
import org.json.*;
String jsonArrayString = "[\"apple\", \"banana\", \"orange\"]";
JSONArray jsonArray = new JSONArray(jsonArrayString);
String firstFruit = jsonArray.getString(0);
String secondFruit = jsonArray.getString(1);
String thirdFruit = jsonArray.getString(2);
```
使用Jackson库解析JSON数组字符串的示例代码:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
String jsonArrayString = "[\"apple\", \"banana\", \"orange\"]";
ObjectMapper objectMapper = new ObjectMapper();
List<String> jsonList = objectMapper.readValue(jsonArrayString, new TypeReference<List<String>>(){});
String firstFruit = jsonList.get(0);
String secondFruit = jsonList.get(1);
String thirdFruit = jsonList.get(2);
```
使用Gson库解析JSON数组字符串的示例代码:
```java
import com.google.gson.*;
String jsonArrayString = "[\"apple\", \"banana\", \"orange\"]";
Gson gson = new Gson();
List<String> jsonList = gson.fromJson(jsonArrayString, new TypeToken<List<String>>(){}.getType());
String firstFruit = jsonList.get(0);
String secondFruit = jsonList.get(1);
String thirdFruit = jsonList.get(2);
```
阅读全文