java使用之json在前端和后台之间的转换

  1.java 中使用json

1.1 导入jar包

如果想在后台java代码中使用json,首先就必须导入json.jar ,json。jar可以去官网上下载,也可以去我的网盘中下载,我的网盘地址为:http://pan.baidu.com/s/1pJKDu11 。

1.2 在java程序中导入类引用

首先将下载好的jar包复制进你的项目中的lib文件夹(一般在webContent-->WEB-INF下,没有就自己新建一个)下,根据不同的开发环境不同,可能有的开发环境只需要将jar包复制进去即可,有的复制进去以后还要选定这个jar包,然后右键,builder Path --> Add Builder Path;

然后在你想使用json对象的java程序中引入下面的类:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
1.3 使用json

通过上面的两个步骤,json就可以在java程序中使用了,使用方法如下:

1.3.1将json格式的string字符串转为json对象

如果想把字符串转化为json对象,首先就要求这个字符串是符合json格式的,json格式可以去网上学习一下,这里不细说,

下面就直接上例子:

String str = "{\"name\":\"caocao\",\"age\":\"32\"}";
		try {
			JSONObject jsonObj = new JSONObject(str);
			System.out.println(jsonObj.getString("name"));
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}

通过上面的一段代码,我们就可以将str字符串转为json对象,并且成功的打印除了转化后的json对象中的name属性的值,这个适合于简单的json数据。

1.3.2将对象转换为json对象
Map<String ,String> params =  new HashMap();
	 
	 params.put("name", "caocao");
	 params.put("age", "30");
	 //声明JSONArray对象并输入JSON字符串
	 JSONArray array = JSONArray.fromObject(params);
	 Systemout.println(array.toString());

通过上面的代码,就可以将一个对象转化为json数组,这个json数组可以用来在向前端传送数据。

1.3.3从json对象中获取json对象值

这个适合于比较复杂的json数据,比如如下代码

String jsonStr = "{\"name\": \"caocao\",\"age\": \"30\",\"identity\": {\"home\": \"father\",\"chief\": \"chief\"}, \"childrens\": [\"caopi\", \"caozhi\", \"caochong\"]}";
		JSONObject jsonObj = new JSONObject(jsonStr);
		JSONObject config = jsonObj.getJSONObject("identity");

这样就可以以json对象形式获取json中某个属性下值仍旧包含属性和值的情况,然后再接着进行其他操作。

1.3.4 从json对象中获取json数组
JSONArray childrens= jsonObj.getJSONArray("childrens");
		for (int index = 0, length = childrens.length(); index < length; index++) {
			 Systemout.println(childrens[i]);
		}
1.3.5将json对象转为string字符串
 //创建JSONObject对象
JSONObject json = new JSONObject();
 //向json中添加数据
json.put("name", "caocao");
json.put("age", 30);
json.put("height", 167);
//创建JSONArray数组,并将json添加到数组
JSONArray array = new JSONArray();
array.put(json);        
//转换为字符串
String jsonStr = array.toString();
System.out.println(jsonStr);
1.3.6将集合转为json对象
public static void main(String[] args)throws JSONException{
        //初始化ArrayList集合并添加数据
        List<String> list = new ArrayList<String>();
        list.add("name");
        list.add("age");
        list.add("sex");
        
        //初始化HashMap集合并添加数组
        Map map = new HashMap();
        map.put("name", "caocao");
        map.put("age", 30);
        
        //初始化JSONArray对象,并添加数据
        JSONArray array = new JSONArray();
        array.put(list);
        array.put(map);
        
        //生成的JSON字符串为:[["name","age","sex"],{"name":69,"age":"30"}]
    }

2.在前端代码中使用json

在前端中使用json必须在页面中添加json.js文件,同样我把这个文件放在我的云盘中,大家可以前去下载:http://pan.baidu.com/s/1c0fInYw ,将此文件复制进webContent下合适(自己决定即可)的文件夹内,然后再网页文件里添加如下引用:

<script src="json/json2.js" type="text/javascript"></script>

注意:上面的src内的内容和你吧这个文件放入哪个文件夹有关,要把对应的这个json.js文件的路径写对才可以。

通过上面的步骤,就可以在页面js函数中使用json对象了。

//@description: 根据接收到的JSON字符串来解析字符串中所包含的数据和数据对象

//接收到的JSON字符串
var result = "[{\"name\": \"coacao\", \"identity\": {\"home\": \"father\", \"army": \"chief\"}}]";
var dataJson = eval("(" + result+ ")");
alert(dataJson.name);
//弹出的对话框为: caocao


转载于:https://my.oschina.net/guopengfei/blog/403952

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值