FastJson:JsonString转化为List<E>源码

二话不说,直接上源代码。转化过程,主要用了Json.parsexx()系列方法,真的是一个负责的中转站。

       //JsonString ----> JsonArray ----> JavaBean
        String jsonStr = FileUtil.readJsonFile(filepath);
        List<MachineConfig> machineConfigList = new ArrayList<>();
        JSONArray jsonArray = JSON.parseArray(jsonStr);
        int count = jsonArray.size();
        for(int i=0; i<count; i++){
            MachineConfig config = JSON.toJavaObject(jsonArray.getJSONObject(i),MachineConfig.class);
            machineConfigList.add(config);
        }

现在时间是2019年09月05日,经过代码实践,我发现FastJson底层支持直接转化为List<>集合,更新后的代码应该长这个样子:

String jsonStr = "your json file";
List<Item> list = JSONObject.parseArray(jsonStr,Item.class);
print(list);

越学习,越感到自己的无知

猜你喜欢

转载自blog.csdn.net/qq_37040173/article/details/99304183