使用阿里开源类库FastJson,当需要处理超大JSON文本时,需要Stream API,在fastjson-1.1.32版本中开始提供Stream API。文档参考GitHub:https://github.com/alibaba/fastjson/wiki/Stream-api
1 /** 2 * 在遇到超大JSON文本数据时使用 3 * @param jsonStr 超长JSON字符串数据 4 * @return 5 * @throws IOException 6 */ 7 public static List<AbstractBean> getBeans(String jsonStr) throws IOException{ 8 List<AbstractBean> abstractBeans = new ArrayList<AbstractBean>(); 9 //读取超大json文本数据 10 StringReader stringReader = new StringReader(jsonStr); 11 JSONReader jsonReader = new JSONReader(stringReader); 12 13 //获取json数据对应的DTO类标记(圣美迪诺:errcode/守望心电:status) 14 String msgStr = configuration.PARSER_SIGNSDATA_FACTORY_MAKE; 15 String[] msgs = msgStr.split(","); 16 AbstractMsg abstractMsg = null; 17 for(String msg : msgs){ 18 if (jsonStr.contains(msg)) { 19 //httpClient返回Response数据接收的DTO类(code、msg、data) 20 abstractMsg = AbstractMsg.getInstance(msg); 21 } 22 } 23 //如果是errcode则转换为圣美迪诺DTO 24 if (abstractMsg instanceof BloodSugarMsg) { 25 BloodSugarMsg bloodSugarMsg = jsonReader.readObject(BloodSugarMsg.class); 26 if(bloodSugarMsg!=null){ 27 abstractBeans = getBloodSugarRecords(bloodSugarMsg); 28 } 29 } 30 //如果是status则转换为守望心电DTO 31 if (abstractMsg instanceof ElectrocardioMsg) { 32 ElectrocardioMsg electrocardioMsg = jsonReader.readObject(ElectrocardioMsg.class); 33 if(electrocardioMsg!=null){ 34 abstractBeans = getElectrocardiograms(electrocardioMsg); 35 } 36 } 37 jsonReader.close(); 38 39 return abstractBeans; 40 }