(1)JSON(Java Script Object Notation(记号,标记))是一种轻量级的数据交换语言,
以文本字符串为基础,且易于让人阅读
注意:XML就是一个重量级的数据交换语言
(2)JSON采用完全独立于任何程序语言的文本格式,使JSON成为理想的数据交换语言
(1)简化创建自定义对象的方式
注意:JSON就是用JS语法来书写,所以必须放在< script>标签中
在用JS语法书写JSON时,最外面不要用”“双引号
var p = {
id:1,
name:"哈哈",
tel:[
{
no:"135",
type:"中移动"
},
{
no:"133",
type:"中联通"
}
],
show:function(username){
alert("你的姓名是:" + p.name+":"+username);
},
isSingle:false
};
var p = {'city':['北京','上海','广州','深圳']};
for(var i=0;i"
");
}
(2)在AJAX中,作为数据载体之一
注意:JS可以直接解析JSON格式的文本,前提是:该JSON必须采用JS格式书写的才行,如果该JSON是采用Java格式写的,必须使用eval()函数转换后,方可被JS解析,该eval(“”)函数接收一个字符串格式的内容,若是一个对象,则eval函数参数必须为eval(“(”+obj+“)”)这种书写格式。
(3)省份-城市-区域三级联动【Struts2 + JSON版】
切记:将来JSON是不能完完全全替代XML的,只能在定义对象和数据交换语言方面替代
action代码:
package province_city_area;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
/** * 省份-城市-区域三级联动【Struts + JSON版】 * Created by Administrator on 2017/2/17. */
public class ProviceCityAreaAction extends ActionSupport {
private Bean bean;
public Bean getBean() {
return bean;
}
public void setBean(Bean bean) {
this.bean = bean;
}
private List cityList;
public List getCityList() {
return cityList;
}
private List areaList;
public List getAreaList() {
return areaList;
}
/** * 根据省份获取城市 */
public String findCityByProvince() throws Exception{
cityList = new ArrayList(){};
if("湖北".equals(bean.getProvince())){
cityList.add("武汉");
cityList.add("赤壁");
}else if("湖南".equals(bean.getProvince())){
cityList.add("长沙");
cityList.add("浏阳");
cityList.add("张家界");
}else if("广东".equals(bean.getProvince())){
cityList.add("广州");
cityList.add("东莞");
cityList.add("佛山");
cityList.add("清远");
}
//让Struts2框架帮你将cityList封装成json
return SUCCESS;
}
/** * 根据城市获取区域 */
public String findAreaByCity() throws Exception{
areaList = new ArrayList(){};
if("武汉".equals(bean.getCity())){
areaList.add("武昌");
areaList.add("汉口");
areaList.add("汉阳");
}else if("赤壁".equals(bean.getCity())){
areaList.add("新店");
areaList.add("六米桥");
areaList.add("赵李桥");
}
//让Struts2框架帮你将cityList封装成json
return SUCCESS;
}
}
struts.xml:
<struts>
<package name="myPackage" extends="json-default" namespace="/">
<global-results>
<result name="success" type="json"/>
global-results>
<action name="checkRequest" class="checkcode.CheckcodeAction" method="check"/>
<action name="findCityByProvinceRequest" class="province_city_area.ProviceCityAreaAction" method="findCityByProvince">
action>
<action name="findAreaByCityRequest" class="province_city_area.ProviceCityAreaAction" method="findAreaByCity">
action>
package>
struts>
需要导入的struts2-json-plugin-2.3.1.1.jar
准备导入第三方jar包:
》commons-beanutils-1.7.0.jar
》commons-collections-3.1.jar
》commons-lang-2.5.jar
》commons-logging-1.1.1.jar
》ezmorph-1.0.3.jar
》json-lib-2.1-jdk15.jar
测试代码:
package bean2json;
import net.sf.json.JSONArray;
import org.junit.Test;
import java.util.*;
/** * Created by Administrator on 2017/2/17. */
public class TestBean2Json {
@Test
public void javabean2json(){
City city = new City(1,"上海");
JSONArray jsonArray = JSONArray.fromObject(city);
String jsonJAVA = jsonArray.toString();
System.out.println(jsonJAVA);
//[{"id":1,"name":"上海"}]
}
@Test
public void list2json(){
List cityList = new ArrayList();
cityList.add(new City(1,"上海"));
cityList.add(new City(2,"广州"));
JSONArray jsonArray = JSONArray.fromObject(cityList);
String jsonJAVA = jsonArray.toString();
System.out.println(jsonJAVA);
//[{"id":1,"name":"上海"},{"id":2,"name":"广州"}]
}
@Test
public void set2json(){
Set citySet = new LinkedHashSet();
citySet.add(new City(1,"上海"));
citySet.add(new City(2,"广州"));
JSONArray jsonArray = JSONArray.fromObject(citySet);
String jsonJAVA = jsonArray.toString();
System.out.println(jsonJAVA);
//[{"id":1,"name":"上海"},{"id":2,"name":"广州"}]
}
@Test
public void javabeanlist2json(){
List cityList = new ArrayList();
cityList.add(new City(1,"北京"));
cityList.add(new City(2,"上海"));
cityList.add(new City(3,"广州"));
Province province = new Province(1,"北上广",cityList);
JSONArray jsonArray = JSONArray.fromObject(province);
String jsonJAVA = jsonArray.toString();
System.out.println(jsonJAVA);
//[{"id":1,"cityList":[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"广州"}],"name":"北上广"}]
}
@Test
public void map2json(){
List cityList = new ArrayList();
cityList.add(new City(1,"北京"));
cityList.add(new City(2,"上海"));
cityList.add(new City(3,"广州"));
Map map = new LinkedHashMap();
map.put("total",cityList.size());
map.put("rows",cityList);
JSONArray jsonArray = JSONArray.fromObject(map);
String jsonJAVA = jsonArray.toString();
System.out.println(jsonJAVA);
//[{"total":3,"rows":[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"广州"}]}]
jsonJAVA = jsonJAVA.substring(1,jsonJAVA.length()-1);
System.out.println(jsonJAVA);
//{"total":3,"rows":[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"广州"}]}
}
}