Ajax的JQuery的实现步骤
1.$.ajax() 实现
function fun() {
var name=$("#pid").val(); //获取输入框的内容
$.ajax({ //创建 Ajax的异步请求
url:"/Servletdeom1", //请求路径
type:"POST", //请求方式
data:{ //传的参数
"name": name
},
success:function (data) { //请求成功过后的执行的 回调函数
alert(data);
}
})
}
2.$.get()
3.$.post()
#GET请求和POST请求是一样的的格式 只是请求不一样
function fun() {
var name=$("#pid").val(); //获取输入框的内容
$.get("Servletdeom1",{name:name},function (date) {
alert(date);
},"text");
}
function fun() {
var name=$("#pid").val(); //获取输入框的内容
$.post("Servletdeom1",{name:name},function (date) {
alert(date);
},"text");
}
JSON
1.概念:Javascrpt objcet notion
如何取josn的值
/* var ponson={name:"张三",age:23,adrest:"四川"};
for(var key in ponson){
alert(key+":"+ponson[key]);
}*/
var posons=[{name:"张三",age:23,adrest:"四川"},
{name:"李四",age:23,adrest:"四川"},
{name:"王五",age:23,adrest:"四川"}
];
for (var i = 0; i <posons.length ; i++) {
var poson = posons[i];
for(var key in poson){
alert(key+":"+poson[key]);
}
}
获取值的方法:
1.JSon对象.键名
2.json对象[“键名”]
3.数组[索引]
json数据和Java对象相互转换
1.json转Java对象
通创建ObjectMapper objectMapper=new ObjectMapper();
String son="{\"grend\":\"男\",\"name\":\"张三\",\"age\":24}";
ponson ponson = objectMapper.readValue(son, ponson.class); //调用readvlue方法
System.out.println(ponson);
输出:ponson{name='张三', age=24, grend='男', bsd=null}
2.Java转json
ponson p=new ponson();
p.setName("张三");
p.setAge(23);
p.setGrend("男");
ObjectMapper objectMapper=new ObjectMapper();
/* String s = objectMapper.writeValueAsString(p);
System.out.println(s);*/
// objectMapper.writeValue(new File("C:\\Users\\86166\\Desktop\\a.txt"),p);
// objectMapper.writeValue(new FileWriter("C:\\Users\\86166\\Desktop\\b.txt"),p);
2.1 注解
2.1.1 @Jsonignore :排除属性
2.1.2 @Jsonformat :属于化的格式
@JsonFormat(pattern = "yyyy-MM-dd") //格式化属性
private Date bsd;
2.2复杂的java对象转换
2.2.1 List
ArrayList<ponson> list=new ArrayList<ponson>();
list.add(p);
list.add(p1);
String s = objectMapper.writeValueAsString(list);
System.out.println(s);
输出结果: [{"name":"张三","age":23,"grend":"男","bsd":"2020-05-16"},{"name":"张三","age":23,"grend":"男","bsd":"2020-05-16"}]
2.2.2 Map
Map<String,Object> map=new HashMap<>();
map.put("name","张三");
map.put("age",24);
map.put("grend","男");
String s = objectMapper.writeValueAsString(map);
System.out.println(s);
输出:{"grend":"男","name":"张三","age":24}
!!!!
补充笔记:
json字符串转json对象的一个转发方法:
对象=$.parseJSON(对象); //将json字符串转成json对象