引入hutool依赖

使用到该依赖的HTTP请求方法

1
2
3
4
5
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.9</version>
</dependency>

HTTP请求的地址为:http://www.zoutl.cn/getFruitListJson ,返回的结果如下图(下面四种方法只能处理中括号[]以及其中的内容,所以要对HTTP请求返回的结果进行截取处理)

img

Fruit实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @author LeDao
* @company
* @create 2021-07-17 22:27
*/
public class Fruit {

private Integer id;

private String name;

private Integer num;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getNum() {
return num;
}

public void setNum(Integer num) {
this.num = num;
}
}

使用GSON

引入依赖

1
2
3
4
5
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) {
//http请求获取json字符串
String str = HttpUtil.get("https://www.zoutl.cn/getFruitListJson");
int start = str.indexOf("[");
int end = str.indexOf("]") + 1;
//截取字符串
str = str.substring(start, end);
System.out.println(str);
//转为list
Gson gson = new Gson();
List<Fruit> fruitList = gson.fromJson(str, new TypeToken<List<Fruit>>(){}.getType());
for (Fruit fruit : fruitList) {
System.out.println(fruit.getId() + "," + fruit.getName() + "," + fruit.getNum());
}
}
}

使用FastJSON

引入依赖

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;

import java.util.List;

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) {
//http请求获取json字符串
String str = HttpUtil.get("https://www.zoutl.cn/getFruitListJson");
int start = str.indexOf("[");
int end = str.indexOf("]") + 1;
//截取字符串
str = str.substring(start, end);
System.out.println(str);
//转为list
List<Fruit> fruitList = JSON.parseArray(str, Fruit.class);
for (Fruit fruit : fruitList) {
System.out.println(fruit.getId() + "," + fruit.getName() + "," + fruit.getNum());
}
}
}

使用JackJSON

引入依赖

1
2
3
4
5
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
//http请求获取json字符串
String str = HttpUtil.get("https://www.zoutl.cn/getFruitListJson");
int start = str.indexOf("[");
int end = str.indexOf("]") + 1;
//截取字符串
str = str.substring(start, end);
System.out.println(str);
//转为list
ObjectMapper objectMapper = new ObjectMapper();
List<Fruit> fruitList = objectMapper.readValue(str, new TypeReference<List<Fruit>>(){});
for (Fruit fruit : fruitList) {
System.out.println(fruit.getId() + "," + fruit.getName() + "," + fruit.getNum());
}
}
}

传统方式org.json

引入依赖

1
2
3
4
5
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import cn.hutool.http.HttpUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) {
//http请求获取json字符串
String str = HttpUtil.get("https://www.zoutl.cn/getFruitListJson");
int start = str.indexOf("[");
int end = str.indexOf("]") + 1;
//截取字符串
str = str.substring(start, end);
System.out.println(str);
//先转为jsonArray
JSONArray jsonArray = new JSONArray(str);
//转为list
List<Fruit> fruitList = new ArrayList<>();
for (Object o : jsonArray) {
//将数组的每个元素转为jsonObject
JSONObject jsonObject = new JSONObject(o.toString());
Fruit fruit = new Fruit();
fruit.setId((Integer) jsonObject.get("id"));
fruit.setName((String) jsonObject.get("name"));
fruit.setNum((Integer) jsonObject.get("num"));
fruitList.add(fruit);
}
for (Fruit fruit : fruitList) {
System.out.println(fruit.getId() + "," + fruit.getName() + "," + fruit.getNum());
}
}
}

结果

img