java中Array/List/Map/Object与Json互相转换详解(一)

2014-11-24 11:36:20 · 作者: · 浏览: 17
JSON(java script Object Notation): 是一种轻量级的数据交换格式
一、JSON建构有两种结构:对象和数组
1、对象:对象在js中表示为“{}”扩起来的内容,数据结构为 {key:value,key:value,...}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。
2、数组:数组在js中是中括号“[]”扩起来的内容,数据结构为 ["java","java script","vb",...],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。
经过对象、数组2种结构就可以组合成复杂的数据结构了。
二、具体形式
1、对象
(1)一个对象以“{”(左括号)开始,“}”(右括号)结束。
(2)每个“名称”后跟一个“:”(冒号)
(3)“‘名称/值’ 对”之间使用“,”(逗号)分隔
例子:表示人的一个对象:
{
"姓名" : "大憨",
"年龄" : 24
}
2、数组是值(value)的有序集合。
(1)一个数组以“[”(左中括号)开始,“]”(右中括号)结束。
(2)值之间使用“,”(逗号)分隔。
例子:一组学生
{
"学生" :
[
{"姓名" : "小明" , "年龄" : 23},
{"姓名" : "大憨" , "年龄" : 24}
]
}
说明:此Json对象包括了一个学生数组,而学生数组中的值又是两个Json对象。
说了这些基本了解json的数据结构了...
三、老样子上次demo
这时我的工程结构图:上面引用到的外部库大家网上搜索 下载~
configdata.json:
[java script]
[
true,
false,
true
]
Address类:
[java]
/** www.2cto.com
* @Title: 创建Address实体类的POJO
* @Description: TODO(用一句话描述该文件做什么)
* @author Potter
* @date 2013-2-18 上午10:16:03
* @version V1.0
*/
public class Address {
private String street;//街道
private String city;//城市
private int zip;//邮编
private String tel;//第一个电话号码
private String telTwo;//第二个电话号码
public Address() {
}
public Address(String street, String city, int zip, String tel, String telTwo){
this.street = street;
this.city = city;
this.zip = zip;
this.tel = tel;
this.telTwo = telTwo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getTelTwo() {
return telTwo;
}
public void setTelTwo(String telTwo) {
this.telTwo = telTwo;
}
}
JsonTest类:
[java]
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONFunction;
import net.sf.json.JSONObject;
public class JsonTest {
public static void main(String args[]) {
//javaArray和json互相转换
javaArrayAndJsonInterChange();
System.out.println("-------------------------------------");
//javaList和json互相转换
javaListAndJsonInterChange();
System.out.println("-------------------------------------");
//javaMpa和Json互转
javaMapAndJsonInterChange();
System.out.println("-------------------------------------");