java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java FastJson

Java基础之FastJson详解

作者:喜羊羊love红太狼

今天给大家复习Java基础FastJson,文中有非常详细的代码示例,对正在学习java的小伙伴们有很好地帮助,需要的朋友可以参考下

一、fastJson将json格式字符串转化成List集合

注:json格式字符串必须符合数组型格式如[{"a":a},{"b":b}]

场景一:前端向后台传递数组格式的json字符串,如何转化成List集合

List<AccountBean> readJson2List =JSON.parseArray(json, AccountBean.class)注意这里是Bean.class而不是List.class

	@Test
	public void readJson2ListBean() {
		String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
		List<AccountBean> readJson2List =JSON.parseArray(json, AccountBean.class);
		//输出测试
		JsonUtils.beanToJson(readJson2List);
	}

场景二:前端向后台传递数组格式的json字符串,如何转化成List<HashMap<String,Object>>集合

	@Test
	public void readJson2ListMap() {
		String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
		List<HashMap> readJson2List =JSON.parseArray(json,HashMap.class);
		//输出测试
		JsonUtils.beanToJson(readJson2List);
	}

二、fastJson将json格式字符串转化成对象

场景一:前端向后台传递数组格式的json字符转化成Bean对象

@Test
	public void readJson2Bean() {
		String json = "{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}";
		AccountBean accountBean  =JSON.parseObject(json,AccountBean.class);
		//输出测试
		JsonUtils.beanToJson(accountBean);
	}

场景二:前端向后台传递数组格式的json字符串Map<String,Object>集合

@Test
	public void readJson2Map() {
		String json = "{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}";
		HashMap<String,Object> accountBean  =JSON.parseObject(json,Map.class);
		//输出测试
		JsonUtils.beanToJson(accountBean);
	}

测试:

三、FastJson将对象或集合转化成json格式字符串

@Test
	public void beanAndCollectionTOJson() {
		AccountBean	bean = new AccountBean();
		bean.setAddress("china-Guangzhou");
		bean.setEmail("hoojo_@126.com");
		bean.setId(1);
		bean.setName("hoojo");
		String json  = JSON.toJSONString(bean);
		System.out.println(json);
	}

四、FastJson将json格式字符串转化成json对象

@Test
	public void jsonStringTOJsonBean() {
		String json = "{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}";
		JSONObject parse = (JSONObject) JSON.parse(json);
		String address = (String) parse.get("address");
		System.out.println("输出address=="+address);
	}

五、FastJson将json格式字符串转化成json数组

	@Test
	public void jsonString2JsonArray() {
		String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
		JSONArray parseArray = JSON.parseArray(json);
		//输出测试
		JsonUtils.beanToJson(parseArray.get(0));
	}

六、FastJson将java对象转化成json对象

@Test
	public void beanAndCollectionTOJsonBean() {
		AccountBean	bean = new AccountBean();
		bean.setAddress("china-Guangzhou");
		bean.setEmail("hoojo_@126.com");
		bean.setId(1);
		bean.setName("hoojo");
		JSONObject json  = (JSONObject) JSON.toJSON(bean);
		json.get("address");
		System.out.println(json);
	}

到此这篇关于Java基础之FastJson详解的文章就介绍到这了,更多相关Java FastJson内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文