JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
名称 / 值对
1
|
{ "firstName": "Brett" }
|
1
|
firstName=Brett
|
1
|
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }
|
表示数组
1
2
3
4
5
6
7
|
{
"people": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
]
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
|
赋值给变量
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
|
访问数据
1
|
people.programmers[0].lastName;
|
1
2
3
|
people.authors[1].genre
// Value is "fantasy"
people.musicians[3].lastName
// Undefined. This refers to the fourth entry, and there isn't one
people.programmers[2].firstName
// Value is "Elliotte"
|
修改数据
1
|
people.musicians[1].lastName =
"Rachmaninov"
;
|
换回字符串
1
|
var
newJSONtext = people.toJSONString();
|
1
|
var
myObjectInJSON = myObject.toJSONString();
|
1
2
3
4
|
{
"姓名" : "大憨",
"年龄" : 24
}
|
1
2
3
4
5
6
7
|
{
"学生" :
[
{"姓名" : "小明" , "年龄" : 23},
{"姓名" : "大憨" , "年龄" : 24}
]
}
|
实例比较
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
country
>
<
name
>中国</
name
>
<
province
>
<
name
>黑龙江</
name
>
<
cities
>
<
city
>哈尔滨</
city
>
<
city
>大庆</
city
>
</
cities
>
</
province
>
<
province
>
<
name
>广东</
name
>
<
cities
>
<
city
>广州</
city
>
<
city
>深圳</
city
>
<
city
>珠海</
city
>
</
cities
>
</
province
>
<
province
>
<
name
>台湾</
name
>
<
cities
>
<
city
>台北</
city
>
<
city
>高雄</
city
>
</
cities
>
</
province
>
<
province
>
<
name
>新疆</
name
>
<
cities
>
<
city
>乌鲁木齐</
city
>
</
cities
>
</
province
>
</
country
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
{
"name":"中国",
"province":[
{
"name":"黑龙江",
"cities":{
"city":["哈尔滨","大庆"]
}
},
{
"name":"广东",
"cities":{
"city":["广州","深圳","珠海"]
}
},
{
"name":"台湾",
"cities":{
"city":["台北","高雄"]
}
},
{
"name":"新疆",
"cities":{
"city":["乌鲁木齐"]
}
}
]
}
|
1.JSONObject介绍
JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包。
2.下载jar包
http://files.cnblogs.com/java-pan/lib.rar
提供了除JSONObject的jar之外依赖的其他6个jar包,一共7个jar文件
说明:因为工作中项目用到的版本是1.1的对应jdk1.3的版本,故本篇博客是基于1.1版本介绍的。
对应此版本的javadoc下载路径如下:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/
目前最新的版本为2.4,其他版本下载地址为http://sourceforge.net/projects/json-lib/files/json-lib/
3.项目环境:
system:WIN7 myeclipse:6.5 tomcat:5.0 JDK:开发环境和编译用的都是1.5
项目结构如下:
说明:本次用到的的文件只有工程目录json包下的JSONObject_1_3类和note.txt
4.class&method 基于1.1的API
做以下几点约定:
1.介绍基于JSONObject 1.1的API
2.只介绍常用的类和方法
3.不再介绍此版本中已经不再推荐使用
4.介绍的类和方法主要围绕本篇博客中用到的
JSONObject:A JSONObject is an unordered collection of name/value pairs.
构造方法如下:
JSONObject();创建一个空的JSONObject对象
JSONObject(boolean isNull);创建一个是否为空的JSONObject对象
普通方法如下:
fromBean(Object bean);静态方法,通过一个pojo对象创建一个JSONObject对象
fromJSONObject(JSONObject object);静态方法,通过另外一个JSONObject对象构造一个JSONObject对象
fromJSONString(JSONString string);静态方法,通过一个JSONString创建一个JSONObject对象
toString();把JSONObject对象转换为json格式的字符串
iterator();返回一个
Iterator对象来遍历元素
接下来就是一些put/get方法,需要普通的get方法和pot方法做一下强调说明,API中是这样描述的:
A get
method returns a value if one can be found, and throws an exception if one cannot be found. An opt
method returns a default value instead of throwing an exception, and so is useful for obtaining optional values.
JSONArray:A JSONArray is an ordered sequence of values.
是一个final类,继承了Object,实现了JSON接口
构造方法如下:
JSONArray();构造一个空的JSONArray对象
普通方法如下:
fromArray(Object[] array);静态方法,通过一个java数组创建一个JSONArray对象
fromCollection(Collection collection);静态方法,通过collection集合对象创建一个JSONArray对象
fromString(String string);静态方法,通过一个json格式的字符串构造一个JSONArray对象
toString();把JSONArray对象转换为json格式的字符串
iterator();返回一个
Iterator对象来遍历元素
接下来同样是put/get方法……
XMLSerializer:Utility class for transforming JSON to XML an back.
一个继承自Object的类
构造方法如下:
XMLSerializer();创建一个
XMLSerializer对象
普通方法如下:
setRootName(String rootName);设置转换的xml的根元素名称
setTypeHintsEnabled(boolean typeHintsEnabled);设置每个元素是否显示type属性
write(JSON json);把json对象转换为xml,默认的字符编码是UTF-8,
需要设置编码可以用
write(JSON json, String encoding)
5.对XML和JSON字符串各列一个简单的例子
JSON:
{"password":"123456","username":"张三"}
xml
<?xml version="1.0" encoding="UTF-8"?>
<user_info>
<password>123456</password>
<username>张三</username>
</user_info>
Code:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONObjectSample {
//创建JSONObject对象
private static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "kevin");
jsonObject.put("Max.score", new Integer(100));
jsonObject.put("Min.score", new Integer(50));
jsonObject.put("nickname", "picglet");
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObjectSample.createJSONObject();
//输出jsonobject对象
System.out.println("jsonObject==>"+jsonObject);
//判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject);
//添加属性
jsonObject.element("address", "swap lake");
System.out.println("添加属性后的对象==>"+jsonObject);
//返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1,"another jsonArray value");
jsonObject.element("jsonArray", jsonArray);
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println("返回一个JSONArray对象:"+array);
//添加JSONArray后的值
//{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
//"jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println(jsonObject);
//根据key返回一个字符串
String jsonString = jsonObject.getString("name");
System.out.println("jsonString==>"+jsonString);
}
}