自己写的代码里用$.get()可以获取到值,但$.getJSON()获取不到返回的值,参考官网api文档上的例子,又可以获取到值了,原因还没找到,先记录下代码,慢慢找原因吧……
//jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="../ajax/jquery-2.1.4.js"></script>
<script type="text/javascript">
$(function(){
$("a").click(function(){
var url = this.href ;
var args = {"time":new Date()};
$.getJSON( url, args, function( result ) {
alert(result.one);
alert(result.two);
alert(result.three);
});
return false;
});
var u = "../ajax/json1.js";
$.getJSON(u, function( data ) {
var items = [];
$.each( data, function( key, val ) {
alert(key + " " + val);
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
})
</script>
<body>
<script>
function test(x){
alert(x.name);
alert(x.age);
alert(x.adress);
}
</script>
<script src="json.js"></script>
<a href="../ajax/json1.js">HelloAjax</a>
<div id="content"></div>
</body>
</html>
//json.json文件
{
"one": "Singular sensation",
"two": "Beady little eyes",
"three": "Little birds pitch by my doorstep"
}
//json.js文件
var obj = {
"name": "Tom","age":12,"adress":"henan",
};
test(obj);
//json1.js文件
{
"one": "Singular sensation",
"two": "Beady little eyes",
"three": "Little birds pitch by my doorstep"
}
仔细又看了看,发现自己写的时候json字符串拼接错误,浪费了这么多时间,都是自己的不细心造成的,醉了……
json标准格式:
{
“ …… ” :“ …… ” ,
“ …… ” :“ …… ” ,
}
使用Jsckson可以轻松转换json字符串,具体例子如下:
步骤:
1、添加jackson的jar包
2、创建ObjectMapper对象
3、调用ObjectMapper对象的writeValueAsString方法把java对象或者集合转为json字符串
注意:
1、jackson根据getter方法来定位JSON对象的属性,而不是字段
2、可以在getter方法上添加注解@JsonIgnore忽略getter方法设置的JSON的属性
package com.atguigu.ajax.app.test;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Book {
private String Name;
private String id;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonIgnore
public String getAuthor() {
return "ya";
}
public Book(String name, String id) {
super();
Name = name;
this.id = id;
}
public static void main(String[] args) throws JsonProcessingException {
//添加jackson的jar包
//创建ObjectMapper对象
//调用ObjectMapper对象的writeValueAsString方法把java对象或者集合转为json字符串
//注意:
//jackson根据getter方法来定位JSON对象的属性,而不是字段
//可以在getter方法上添加注解@JsonIgnore忽略getter方法设置的JSON的属性
ObjectMapper mapper = new ObjectMapper();
Book book = new Book("guo", "01");
String jsonStr = mapper.writeValueAsString(book);
System.out.println(jsonStr);
List<Book> books = Arrays.asList(book, new Book("ma", "02"));
jsonStr = mapper.writeValueAsString(books);
System.out.println(jsonStr);
}
}