当前位置:编程学习 > JAVA >>

js 数组 在java中怎么读取 急啊!

追问:在JAVA中我取出来的是这种,我不知道怎么把它转换成对象。[{plbm:reference:c0-e15, pwmc:reference:c0-e16, bzjg:reference:c0-e17, orgid:reference:c0-e18, sl:reference:c0-e19, zje:reference:c0-e20}, {plbm:reference:c0-e22, pwmc:reference:c0-e23, bzjg:reference:c0-e24, orgid:reference:c0-e25, sl:reference:c0-e26, zje:reference:c0-e27}]不是固定的呢
答案:javascript是前台的脚本语言、java不能直接读取、你可以将js的数组以一个特殊符号分割组成一个字符串传到后台在用java的split方法转换成数组!
比如:
js code: var arr = {'a','b','c'}; => var ss = “a&b&c”;
java code:String ss = "a&b&c";String[] arr = ss.split('&');
其他:request.getParamValues("array");
如果你里面放的是固定类的话就用json 不固定的也可以用json啊,json只是一种数据的表达形式,跟内容无关,具体对内容的操作都是你JAVA代码完成的,js只负责把数据格式化成固定的格式让后台接收而已 json,或者字符串处理,当然,你要通过FORM传值也可以 你可以把接收的字符串“[{plbm:reference:c0-e15, pwmc:reference:c0-e16, bzjg:reference:c0-e17, orgid:reference:c0-e18, sl:reference:c0-e19, zje:reference:c0-e20}, {plbm:reference:c0-e22, pwmc:reference:c0-e23, bzjg:reference:c0-e24, orgid:reference:c0-e25, sl:reference:c0-e26, zje:reference:c0-e27}]”转换成二维数组,然后操作这个二维数组就可以了。

String s = "[{plbm:reference:c0-e15, pwmc:reference:c0-e16, bzjg:reference:c0-e17, orgid:reference:c0-e18, sl:reference:c0-e19, zje:reference:c0-e20}, {plbm:reference:c0-e22, pwmc:reference:c0-e23, bzjg:reference:c0-e24, orgid:reference:c0-e25, sl:reference:c0-e26, zje:reference:c0-e27}]";
String [] arr = s.split("}");
String [][] arr2 = new String[arr.length - 1][arr[0].split(",").length];
for (int i = 0; i < arr.length - 1; i ++) {
    String[] s1 = arr[i].split(",");
    for (int j = 0; j < s1.length; j ++) {			
	if (i > 0) {
	    if (j > 0) {
		if (j == 1) {
		    arr2[i][j - 1] = s1[j].substring(2);
		} else {
		    arr2[i][j - 1] = s1[j];
		}
	    }
	} else {
	    if (j == 0) {
		arr2[i][j] = s1[j].substring(2);
	    } else {
		arr2[i][j] = s1[j];
	    }
	}
    }
}

上一个:求java高手
下一个:为什么我改变了环境变量(java用)后,启动电脑就显示您无权更改Catalyst Control Center

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,