jq 解析josn字符串
1. var obj = jQuery.parseJSON("${ruleModel.rules}"); 2. var obj = eval("("+"${ruleModel.rules}"+")");
//上面两种方式解析josn 字符串,第一种要求规范的josn格式(要求键和值 都要有双引号),第二种能够满足非规范格式的josn字符串 if(obj[i].field=="EXPECTED_INCOME") //利率 { var val; val=obj[i].value.replace('[',''); val=val.replace(']',''); var arr= val.split(','); $("input[name='minRate']").val(arr[0]); $("input[name='maxRate']").val(arr[1]); } if(obj[i].field=="OBTAIN_CHANNEL") //资产来源 { var val; val=obj[i].value.replace('[',''); val=val.replace(']',''); var arr= val.split(','); for(var j=0;j<arr.length;j++) { $("input[name='OBTAIN_CHANNEL']").each(function(){ if($(this).val()==arr[j]) { $(this).attr("checked", true); } }); } }
3.ajax 的 dataType: 'json',
function uploadFile(obj, type) {
var fd = new FormData();
fd.append("upload", 1);
fd.append("file", $("#fileUpload1").get(0).files[0]);
$.ajax({
url: "../UploadHandler.ashx",
type:"post",
processData: false,
contentType: false,
dataType: 'json',// 返回值类型 一般设置为json
data: fd,
success: function (data) // 服务器成功响应处理函数
{
console.log(data);
console.log(data.Msg);
},
error: function (data)// 服务器响应失败处理函数
{
console.log("服务器异常");
}
});
return false;
}
数据格式: [ { "optionvalue": "1", "name": "1" }, { "optionvalue": "2", "name": "2" }, { "optionvalue": "2016款全新胜达", "name": "2016款全新胜达" }, { "optionvalue": "3", "name": "3" }, { "optionvalue": "4", "name": "4" }, { "optionvalue": "5", "name": "5" }, { "optionvalue": "55", "name": "55" }, { "optionvalue": "6", "name": "6" }, { "optionvalue": "che1", "name": "che1" }, { "optionvalue": "dazhong1", "name": "dazhong1" }, { "optionvalue": "dazhong2", "name": "dazhong2" }, { "optionvalue": "dazhong3", "name": "dazhong3" }, { "optionvalue": "dazhong4", "name": "dazhong4" }, { "optionvalue": "YC", "name": "YC" }, { "optionvalue": "yuena", "name": "yuena" }, { "optionvalue": "宝马", "name": "宝马" }, { "optionvalue": "宝马宝马", "name": "宝马宝马" }, { "optionvalue": "奔驰", "name": "奔驰" }, { "optionvalue": "测试车", "name": "测试车" }, { "optionvalue": "测试车1", "name": "测试车1" }, { "optionvalue": "车10", "name": "车10" }, { "optionvalue": "车3", "name": "车3" }, { "optionvalue": "车4", "name": "车4" }, { "optionvalue": "车5", "name": "车5" }, { "optionvalue": "车6", "name": "车6" }, { "optionvalue": "汉兰达", "name": "汉兰达" }, { "optionvalue": "朗动", "name": "朗动" }, { "optionvalue": "领动", "name": "领动" }, { "optionvalue": "领动3", "name": "领动3" }, { "optionvalue": "领动4", "name": "领动4" }, { "optionvalue": "名图", "name": "名图" }, { "optionvalue": "全新胜达", "name": "全新胜达" }, { "optionvalue": "全新途胜", "name": "全新途胜" }, { "optionvalue": "锐纳", "name": "锐纳" }, { "optionvalue": "瑞奕", "name": "瑞奕" }, { "optionvalue": "索九", "name": "索九" }, { "optionvalue": "索纳塔", "name": "索纳塔" }, { "optionvalue": "雪佛兰", "name": "雪佛兰" }, { "optionvalue": "雪铁龙", "name": "雪铁龙" }, { "optionvalue": "悦动", "name": "悦动" }, { "optionvalue": "悦纳", "name": "悦纳" } ] //-------------------------------- var url = "@Url.Action("GetCarCategoryList")"; $.ajax({ type: "post", //async: false, //同步 url: url, success: function (result) { //var obj = eval("(" + result + ")"); //var josn = eval(result); var obj = JSON.parse(result); var htmlstr = " <option value=''>请选择</option>"; if (obj.length > 0) { for (var i = 0; i < obj.length; i++) { htmlstr += "<option value='" + obj[i].optionvalue + "'>" + obj[i].name + "</option>"; } } $("#CarCategory").html(htmlstr); } });