Java环境:
一.从字符串反序列化为List
OCR系统返回的字符串格式为:
{"code": "1", "data": [[{"page_id": "1", "field_name": "u4e00u6bb5", "field_id": 1, "slice_name": "slice_0.png", "text": "320"}]], "message": ""}
怎样反序列化呢?方法如下:
String body = "..."; JSONObject jsonObject = JSON.parseObject(body);
然后,判断code是否正常返回:
String result = jsonObject.get("code").toString(); if (result.equals("1")) { String dataString = jsonObject.get("data").toString(); dataString = dataString.substring(1,dataString.length()-1); JSONArray objects = JSONArray.parseArray(dataString); List<ParseResult> ocrParseResults = objects.toJavaList(ParseResult.class); }
而ParseResult类就是你自定义的业务实体类,这样既可!
引用所需为:
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
二.从Map反序列化为List
需要解析的参数:
@RequestBody Map<String, Object> infoMap
方法如下:
List<Map<String, Object>> processRecords = (List<Map<String, Object>>) infoMap.get("processRecords"); for (int i = 0; i < processRecords.size(); i++) { List<ProcessRecord> processRecordList = (List<ProcessRecord>) processRecords.get(i).get("RecordList"); Object processRecordListJson = JSON.toJSON(processRecordList); JSONArray objects = JSONArray.parseArray(processRecordListJson.toString()); List<Record> ocrParseResults = objects.toJavaList(Record.class); }