Java读取JSON文件获取name等属性值问题求助
问题解决:Java解析JSON并按要求输出指定字段
原JSON文件(test.json)
{ "names": [ { "name": "ABC", "thingTypeName": "SmartPhone", "attributes": { "MACAddress": "02:00:00:44:11:30", "active": "true" }, "version": 1 }, { "name": "XYZ", "thingTypeName": "SmartPhone", "attributes": { "active": "true", "uniqueDeviceIdentifier": "2EAFCEE9-6379-4010-B6B1-8F335D83316F" }, "version": 38 }, { "name": "YYZ", "thingTypeName": "SmartPhone", "attributes": { "active": "true", "uniqueDeviceIdentifier": "2EAFCEE9-6379-4010-B6B1-8F335D833161" }, "version": 39 }, { "name": "AAA", "thingTypeName": "SmartPhone", "attributes": { "MACAddress": "02:00:00:44:55:32" }, "version": 1 } ] }
现有问题代码
JSONParser jsonParser = new JSONParser(); Object object; try { object = jsonParser.parse(new FileReader("src/main/resources/Test.json")); JSONObject jsonObject = (JSONObject) object; JSONArray things = (JSONArray) jsonObject.get("things"); Iterator itr = things.iterator(); System.out.println("thing name"+"^"+"mac Address"+"^"+"activation Code"+"^"+"unique Device Identifier"); while (itr.hasNext()) { Object slide = itr.next(); JSONObject jsonObject2 = (JSONObject) slide; JSONObject attributes = (JSONObject) jsonObject2.get("attributes"); String macAddress = (String) attributes.get("MACAddress"); String activationCode = (String) attributes.get("activationCode"); String uniqueDeviceIdentifier = (String) attributes.get("uniqueDeviceIdentifier"); System.out.println(macAddress+"^"+activationCode+"^"+uniqueDeviceIdentifier); }
期望输出
name^MACAddress^active^uniqueDeviceIdentifier ABC^02:00:00:44:11:30^true^null XYZ^null^true^2EAFCEE9-6379-4010-B6B1-8F335D83316F YYZ^null^true^2EAFCEE9-6379-4010-B6B1-8F335D833161 AAA^02:00:00:44:55:32^null^null
修正后的代码
import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; public class JsonParserDemo { public static void main(String[] args) { JSONParser jsonParser = new JSONParser(); try (FileReader reader = new FileReader("src/main/resources/test.json")) { // 解析JSON文件 JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); // 获取正确的数组节点:names(原代码错写为things) JSONArray things = (JSONArray) jsonObject.get("names"); Iterator itr = things.iterator(); // 输出正确表头 System.out.println("name^MACAddress^active^uniqueDeviceIdentifier"); while (itr.hasNext()) { JSONObject item = (JSONObject) itr.next(); // 直接从当前节点获取name属性(不在attributes里) String name = (String) item.get("name"); JSONObject attributes = (JSONObject) item.get("attributes"); // 获取各属性,缺失时返回null String macAddress = (String) attributes.get("MACAddress"); String active = (String) attributes.get("active"); String uniqueDeviceIdentifier = (String) attributes.get("uniqueDeviceIdentifier"); // 按格式拼接输出,null直接显示 System.out.printf("%s^%s^%s^%s%n", name, macAddress == null ? "null" : macAddress, active == null ? "null" : active, uniqueDeviceIdentifier == null ? "null" : uniqueDeviceIdentifier); } } catch (IOException | ParseException e) { e.printStackTrace(); } } }
关键修正点
- 数组节点错误:原代码中
jsonObject.get("things")应改为jsonObject.get("names"),与JSON文件中的数组键名匹配。 - name属性获取:
name字段在每个数组元素的根层级,直接从item.get("name")获取,而非attributes对象。 - 属性名匹配:原代码中的
activationCode对应JSON里的active,需修正属性名。 - 缺失字段处理:当属性不存在时,主动显示
null,保证输出格式统一。 - 资源管理:使用
try-with-resources自动关闭文件读取流,避免资源泄漏。
内容的提问来源于stack exchange,提问作者Prasad
相关产品推荐
相关产品推荐

