当我们在做项目的时候有时候会对接第三方的接口,假如对方返回的结果是一个不规则的json字符串,这个时候我们就需要对于返回结果一条一条的进行读取并操作:
一、json例子:不是数组,单纯的只是一行一行的数据
{"name":"小明"}
{"name":"小红","age":18}
{"name":"小李","sex":"man"}
二、将jsonString传入方法中进行读取行来操作:
public void jsonLineOperate(String jsonString) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(jsonString.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));String line;while ((line = br.readLine()) != null) {if (!line.trim().equals("")) {ObjectMapper mapper = new ObjectMapper();Map<String, Object> map = mapper.readValue(line, Map.class);//todo 针对line的每行数据做操作//.....}}}