AJAX
请求一个页面
//JS
function fn(w){
$.ajax({
type: 'post',
url: w,
dataType: 'html',
success: function (result) {
$('#MasterContent').html(result);
}
});
}
//后台
@RequestMapping("/pc/gouxuan")
public String fpGouxuan(){
return "sjpc/gouxuanPc";
}
请求一个数据
//JS
$.ajax({
"type":"POST",
"url": "pc/GsList.do",
"data": null,
"dataType": "json",
"success": function(res) {}
});
//后台
@RequestMapping("/pc/GsList")
@ResponseBody
public Json getGsList(HttpServletRequest req){
List<HashMap<String,String>> gsList = new ArrayList<HashMap<String,String>>();
String tel = "18108118251";
gsList = byTelService.getGSList(tel);
//获取该电话号码的管理的公司列表【公司名称,纳税人识别号,纳税人社会信用代码】
System.out.println("*****************"+gsList.size()+"************");
Json json = new Json();
json.setObj(gsList);
json.setMsg("公司列表");
json.setSuccess(true);
return json;
}
将JSON字符串转化为Map或List<Map>
引入包:
json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:commons-beanutils.jar,commons-
httpclient.jar,commons-lang.jar,ezmorph.jar,morph-1.0.1.jar
import net.sf.json.JSONArray;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
//*******************************转化为List<Map>*******************
/**
* @Title: jsonToListMap
* @Description: 将获取的json字符串数组转化为list<Map>
* @param: @param jsonStr
* @param: @return
* @return: List<Map<String,String>>
* @throws
*/
//将jsonStr转化为List<Map> jsonStr输出如下:
String jsonStr = "[{
"GFMC": "2",
"FPHM": "2",
"XFSH": "2",
"FPDM": "2",
"ROW_ID": 2,
"XFMC": "2",
"JE": 2,
"GXZT": 2,
"GFSH": "2",
"KPRQ": "2"
}, {
"GFMC": "a1",
"FPHM": "a1",
"XFSH": "a1",
"FPDM": "a1",
"ROW_ID": "a1",
"XFMC": "a1",
"JE": "a1",
"GXZT": 0,
"GFSH": "a1",
"KPRQ": "a1"
}]";
public List<Map<String, String>> jsonToListMap(String jsonStr){
JSONArray json=JSONArray.fromObject(jsonStr);
List<Map<String,String>> listMaps = new ArrayList<Map<String,String>>();
GsonBuilder gb = new GsonBuilder();
Gson g = gb.create();
for(int i=0;i<json.size();i++){
Map<String, String> map = g.fromJson(json.get(i).toString(), new TypeToken<Map<String, String>>() {}.getType());
listMaps.add((Map<String, String>) map);
}
for(int j=0;j<listMaps.size();j++){
System.out.println(listMaps.get(j).toString());
}
return listMaps;
}
//************************************转化为map******************
//将{"fpdm":"","fphm":"","dateCheck":"","xfsh":"","sfgx":"1"}转换为map
GsonBuilder gb = new GsonBuilder();
Gson g = gb.create();
Map<String, String> map = g.fromJson(pd.get("search").toString(), new TypeToken<Map<String, String>>() {}.getType());
网友评论