二维数组转化为json

例: 二维数组格式 1,手机进水,123,321,1,手机进水,123,321
/** 
     *js数组转json 
     * 
     */  
    function arrayToJson(o) {   
    var r = [];   
    if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";   
    if (typeof o == "object") {   
    if (!o.sort) {   
    for (var i in o)   
    r.push(i + ":" + arrayToJson(o[i]));   
    if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {   
    r.push("toString:" + o.toString.toString());   
    }   
    r = "{" + r.join() + "}";   
    } else {   
    for (var i = 0; i < o.length; i++) {   
    r.push(arrayToJson(o[i]));   
    }   
    r = "[" + r.join() + "]";   
    }   
    return r;   
    }   
    return o.toString();   
    }  
调这个JS方法转化后   为  [["1","手机进水","123","321"],["1","手机进水","123","321"]]
然后在Java里转化为具体的对象 ,封装成你需要的格式就行了
JSON需要下载一个jar包:
下载地址:http://code.alibabatech.com/svn/fastjson/repository/com/alibaba/fastjson/1.0.4/fastjson-1.0.4.jar


	public List JsonToList(String arr){
		List relReceiveFaultList = new ArrayList<RelChecklistFault>();
		com.alibaba.fastjson.JSONArray mainArray=JSON.parseArray(arr);  
		 for(int i=0;i< mainArray.size();i++){  
			 log.info("数组大小:"+mainArray.size()); 
	           com.alibaba.fastjson.JSONArray paramObject=(com.alibaba.fastjson.JSONArray)mainArray.get(i);  
	           RelChecklistFault  relChecklistFault= new RelChecklistFault();
	           relChecklistFault.setFaultType(paramObject.get(0).toString());
	           relChecklistFault.setFaultSymptom(paramObject.get(1).toString());
	           relChecklistFault.setFaultCause(paramObject.get(2).toString());
	           relChecklistFault.setSuggest(paramObject.get(3).toString());
			  relReceiveFaultList.add(relChecklistFault);
	       }  
		return relReceiveFaultList;
	}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页