JSONString、JSONArray、JSONObject、JsonNode转换及遍历

JSONString、JSONArray、JSONObject、JsonNode转换及遍历

    • 代码示例:
    • User
    • Password

代码示例:

public class JsonToString {

private Map<String, Object> map;
private User user;

public void init(int num) {
	    map = new HashMap<>();
	    user = new User();
	    user.setUserName("zhangsan");
	    user.setAge(11);
	    user.setTTL(System.currentTimeMillis());
	    List<Password> pwdList = new ArrayList<>();
	    for (int i = 0; i < num; i++) {
	        Password password = new Password();
	        password.setPassword("pass" + i);
	        password.setStatus(i);
	        password.setMac("11-22-3" + i);
	        pwdList.add(password);
	    }
	    user.setPasswords(pwdList);
	}
	
	public void jsonToString() {
	    init(3);
	    //JavaBean转JSONString
	    String userJsonStr = JSON.toJSONString(user);
	    System.out.println(userJsonStr);
	
	    //JSONString转JSONArray
	    JSONArray userJsonArray = JSON.parseArray(JSON.toJSONString(user.getPasswords()));
	    System.out.println(userJsonArray);
	
	    //JSONString转JSONObject
	    JSONObject parseObject = JSON.parseObject(userJsonStr);
	    System.out.println(parseObject);
	
	    //JSONString转JavaBean
	    User parseUser = JSON.parseObject(userJsonStr, User.class);
	    System.out.println("parseUser = "+parseUser);
	
	    //JSONObject转JavaBean
	    User user = parseObject.toJavaObject(User.class);
	    System.out.println("user = "+user);
	
	    //JSONArray遍历
	    if (userJsonArray.size() >= 0) {
	        for (int i = 0; i < userJsonArray.size(); i++) {
	            JSONObject password = userJsonArray.getJSONObject(i);
	            System.out.println(password);
	        }
	    }
	
	    //JSONObject遍历
	    JSONObject userJsonObj = JSON.parseObject(userJsonStr);
	    Set<Map.Entry<String, Object>> entries = userJsonObj.entrySet();
	    for (Map.Entry<String,Object> entry : entries){
	        System.out.println("key = "+entry.getKey()+",value = "+entry.getValue());
	    }
	
	    //JSONString转JsonNode
	    ObjectMapper mapper = new ObjectMapper();
	    JsonNode userJsonNode = null;
	    try {
	        userJsonNode = mapper.readTree(userJsonStr);
	        System.out.println(userJsonNode);
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	
	    //JsonNode遍历keys
	    Iterator<String> keyIterator = userJsonNode.fieldNames();
	    while(keyIterator.hasNext()){
	        String key = keyIterator.next();
	        System.out.println("key = "+key);
	    }
	
	    //JsonNode遍历values
	    Iterator<JsonNode> elements = userJsonNode.elements();
	    while(elements.hasNext()){
	        JsonNode next = elements.next();
	        System.out.println("jsonNode = "+next);
	    }
	
	    //JsonNode遍历key和value键值对
	    Iterator<Map.Entry<String, JsonNode>> fields = userJsonNode.fields();
	    while(fields.hasNext()){
	        Map.Entry<String, JsonNode> entry = fields.next();
	        System.out.println("entry = "+entry);
	    }
	}
}

User

public class User { 
	private String userName; 
	private int age; 
	private List<Password> passwords; 
	private long TTL;
}

Password

public class Password { 
	private String password; 
	private String mac; 
	private int status;
}

你可能感兴趣的:(Java)