第一种方式: pom依赖: <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20210307</version> </dependency>
代码如下: String json = "你的json串"; try { JSONObject jsonObject = new JSONObject(json); Iterator<String> it = jsonObject.keys(); while (it.hasNext()) { String key = it.next(); Object o = jsonObject.get(key); System.out.println("key:" + key + ",value:" + o.toString()); } } catch (JSONException e) { e.printStackTrace(); }
第二种方式:
pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.38</version> </dependency>
String jsonstr = "你的json串"; JSONObject jsonObject = JSON.parseObject(jsonstr); Set<String> strings = jsonObject.keySet(); for (String str : strings) { System.out.println("key:" + str + " value:" + jsonObject.get(str)); }