JSON 修改问题:浅拷贝 和 深拷贝

JSON 修改问题:浅拷贝 和 深拷贝


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUpdateProblem {

	// json 在 add 的时候,会被拷贝。
	public static void main(String[] args) throws Exception {
		jsonUpdate(args);
		System.out.println();
		System.out.println();
		javaUpdate(args);
	}
	
	public static void jsonUpdate(String[] args) throws Exception {

		String originJsonText = "[{\"name\": \"jinan\", \"gdp\": 123},{\"name\": \"qingdao\", \"gdp\": 456}]";
		JSONArray citiesJsonArray = JSONArray.fromObject(originJsonText);
		
		JSONObject jinanJson = citiesJsonArray.getJSONObject(0); // get 浅拷贝(共享)
		System.out.println("jinan origin << " + jinanJson);
		System.out.println("cities origin << " + citiesJsonArray);
		
		System.out.println();
		jinanJson.element("gdp", "321");
		System.out.println("jinan changed >> " + jinanJson);
		System.out.println("cities changed ? >> " + citiesJsonArray); // true
		
		
		System.out.println();
		JSONObject hezeJson = JSONObject.fromObject("{\"name\": \"heze\", \"gdp\": 789}");
		citiesJsonArray.add(hezeJson); // add 深拷贝(复制)
		System.out.println("cities after heze added >> " + citiesJsonArray);
		
		System.out.println();
		hezeJson.element("gdp", 987);
		System.out.println("heze updated >> " + hezeJson);
		System.out.println("cities after heze updated ? >> " + citiesJsonArray); // false
		
		// [{"name":"jinan","gdp":"321"},{"name":"qingdao","gdp":456},{"name":"heze","gdp":789}]
		// heze 数据依然是 789,并没有发生变化。这种特性跟JAVA对象是不一样的。
	}
	
	public static void javaUpdate(String[] args) throws Exception {
		
		List<City> cities = new ArrayList<>(Arrays.asList(new City("jinan", 123), (new City("qingdao", 456))));
		City jinan = cities.get(0); // get 浅拷贝 (共享)
		System.out.println("jinan origin << " + jinan);
		System.out.println("cities origin << " + cities);
		
		
		System.out.println();
		jinan.setGdp(321);
		System.out.println("jinan changed >> " + jinan);
		System.out.println("cities changed ? >> " + cities); // true

		System.out.println();
		City heze = new City("heze", 789);
		cities.add(heze); // add 浅拷贝(共享)
		System.out.println("cities after heze added >> " + cities);
		
		System.out.println();
		heze.setGdp(987);
		System.out.println("heze updated >> " + heze);
		System.out.println("cities after heze updated ? >> " + cities); // true
		// [City [name=jinan, gdp=321], City [name=qingdao, gdp=456], City [name=heze, gdp=987]]
		// heze 数据被一同修改成 987
	}
	
	
	static class City {
		private String name;
		private Integer gdp;
		
		public City(String name, Integer gdp) {
			super();
			this.name = name;
			this.gdp = gdp;
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Integer getGdp() {
			return gdp;
		}
		public void setGdp(Integer gdp) {
			this.gdp = gdp;
		}

		@Override
		public String toString() {
			return "City [name=" + name + ", gdp=" + gdp + "]";
		}
		
	}

}


JSON-lib



  1. <dependencies>  
  2. <span style="white-space:pre">    </span><dependency>  
  3.      <span style="white-space:pre">   </span><groupId>net.sf.json-lib</groupId>  
  4.         <artifactId>json-lib</artifactId>  
  5.         <version>2.4</version>  
  6.         <classifier>jdk15</classifier>    
  7.     </dependency>  
  8. </dependencies>

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值