Java使用Fastjson把Json格式的数据转为LinkedTreeMap

2023年9月24日 380点热度 0人点赞 0条评论

可以使用fastjson将json格式的数据转换为LinkedTreeMap。LinkedTreeMap是fastjson库中的一种数据结构,它继承了HashMap,并且内部使用链表维护了键值对的顺序。以下是一个使用fastjson将json转换为LinkedTreeMap的示例代码:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

public class Example {
    public static void main(String[] args) {
        String jsonStr = "{\"error\":{\"message\":\"Rate limit reached for default-gpt-3.5-turbo in organization org-dLvKMz1rANYmskgaO2YNycYQ on requests per day. Limit: 200 / day. Please try again in 7m12s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method.\",\"type\":\"requests\",\"param\":null,\"code\":\"rate_limit_exceeded\"}}";

        // 使用fastjson将json转为LinkedTreeMap
        LinkedTreeMap<String, Object> linkedTreeMap = JSON.parseObject(jsonStr, new TypeReference<LinkedTreeMap<String, Object>>(){});

        // 输出结果
        System.out.println(linkedTreeMap);
    }
}

运行以上代码,你将得到将json字符串转换为LinkedTreeMap的结果。

小小调酒师

此刻打盹,你将做梦; 此刻学习,你将圆梦。 个人邮箱:shellways@foxmail.com

文章评论