不同第三方jar对json串的解析效果不同。
1. json包
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
<!-- 2018版本提供了JSONObject的toMap方法 -->
</dependency>
字符串变量名 data
import org.json.JSONArray; import org.json.JSONObject; JSONArray arr = new JSONArray(data); Iterator<Object> i = arr.iterator(); while(i.hasNext()){ JSONObject obj = (JSONObject)i.next(); System.out.println("-----------------------------------------------------------"); Set<String> set = obj.keySet(); Iterator<String> i2 = set.iterator(); while(i2.hasNext()) { String key = i2.next(); System.out.println(key+" "+obj.getString(key)); } }
2.jackson
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.4</version> </dependency>
字符串变量名 data
ObjectMapper objMapper = new ObjectMapper(); ArrayList<Map<String,Object>> list = objMapper.readValue(data,ArrayList.class); for(Map<String,Object> map : list) { Set<Map.Entry<String, Object>> set = map.entrySet(); Iterator<Map.Entry<String, Object>> i = set.iterator(); while(i.hasNext()) { Map.Entry<String, Object> entry = i.next(); System.out.println(entry.getKey()+" "+entry.getValue()); } }