import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class RefFastJson {
public static void main(String[] args) {
// 模拟返回的数据
String str = "{
\n" +
" \"code\": 200,\n" +
" \"data\": {
\n" +
" \"users\": [\n" +
" {
\n" +
" \"name\": \"死灵\",\n" +
" \"sex\": \"男\",\n" +
" \"age\": 18\n" +
" },\n" +
" {
\n" +
" \"name\": \"骷髅\",\n" +
" \"sex\": \"男\",\n" +
" \"age\": 20\n" +
" },\n" +
" {
\n" +
" \"name\": \"小黑\",\n" +
" \"sex\": \"女\",\n" +
" \"age\": 38\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"msg\": \"操作成功\"\n" +
"}";
// 开始使用 JSONObject 处理数据
JSONObject jsonObject = JSONObject.parseObject(str);
JSONObject data = jsonObject.getJSONObject("data");
String users = data.getString("users");
List<TUser> userList = JSONObject.parseArray(users, TUser.class);
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
// TODO 此为情况1,即 HashMap 创建在循环外面
HashMap<String, Object> hashMap = new HashMap<>();
userList.forEach((user) -> {
// TODO 此为情况2,即 HashMap 创建在循环里面
// HashMap<String, Object> has
JSON.toJSONString出现{“$ref“:“$[0]“}
于 2023-03-27 22:16:17 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)