需要导入这些包
commons-beanutils-1.7.0.jar
commons-lang-2.1.jar
ezmorph-1.0.2.jar
json-lib-1.1-jdk15.jar
一、JSON介绍
JSON(JavaScript Object Notation),类似于XML,是一种数据交换格式,比如JAVA产生了一个数据想要给JavaScript,则除了利用XML外,还可以利用JSON;
JSON相比XML的优势是表达起来很简单;
官网:http://www.json.org/
JSON是AJAX中的X(就是可以取代XML); ------出自JSON创始人;
注意:JSON并不是一个文档格式,没有*.json的文档,一般JSON格式的文档存在txt中,而XML可以是一个标准;
二、JSON数据结构
JSON有两种数据结构:
(1)Map,也称为对象;{....}
(2)Array;[......]
也就是说所有的JSON对象都是要以这些形式表示;
1.Map
简单的说就是Java中的Map,名称-值对的形式给出,名称和值之间用“:”隔开,两个Map之间用“,”隔开,一般表示形式如下:
{'key1':'value1','key2':'value2'}
以下为官网的图:
2.Array
就是普通意义上的数组,一般形式如下:
['arr1','arr2','arr3'];
以下为官网的图:
图中的value可以是:
总结:
(1)JSON的数据结构只有两种;
(2)可以嵌套表示,比如Array中可以嵌套Object等;
(3)记住:Object是以{}表示,Array是以[ ]表示;
三、JSON与XML互转示例
前面提到任何的XML都能够转换成JSON包;
1.简单XML
XML格式:
view plaincopy to clipboardprint?<person>
<name>xiazdong</name>
<age>20</age>
</person>
<person>
<name>xiazdong</name>
<age>20</age>
</person>
JSON格式:
view plaincopy to clipboardprint?{
"name":"xiazdong",
"age":20
}
{
"name":"xiazdong",
"age":20
}
2.复杂XML
XML格式:
view plaincopy to clipboardprint?<section>
<title>BOOK</title>
<signing>
<author name="author-1"/>
<book title="book1" price="$11"/>
</signing>
<signing>
<author name="author-2"/>
<book title="book2" price="$22"/>
</signing>
</section>
<section>
<title>BOOK</title>
<signing>
<author name="author-1"/>
<book title="book1" price="$11"/>
</signing>
<signing>
<author name="author-2"/>
<book title="book2" price="$22"/>
</signing>
</section>
JSON格式:
view plaincopy to clipboardprint?"section":{
"title":"BOOK",
"signing":[
{
<span style="white-space:pre"> </span> "author":
{
"name":"author-1"
},
"book":
{
"title":"book1",
"price":"$11"
}
},
{
"author":
{
"name":"author-2"
},
"book":
{
"title":"book2",
"price":"$22"
}
}
<span style="white-space:pre"> </span> ]}
}
"section":{
"title":"BOOK",
"signing":[
{
<span style="white-space:pre"> </span> "author":
{
"name":"author-1"
},
"book":
{
"title":"book1",
"price":"$11"
}
},
{
"author":
{
"name":"author-2"
},
"book":
{
"title":"book2",
"price":"$22"
}
}
<span style="white-space:pre"> </span> ]}
}
注意:XML中的属性也是通过JSON的Map表示;
四、JSON包
如果我们想要使用JSON包,则可以下载JSON包的源代码,并将这些代码添加进Eclipse工程中,即可调用;
如果想要看API文档,可以参见:http://www.JSON.org/java/index.html
JSON包中最常用的两个类就是JSONObject和JSONArray,分别表示两种数据结构;
1.JSONObject代码实例
view plaincopy to clipboardprint?package com.xiazdong.json;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
public class Test {
public static void main(String[] args) throws Exception{
JSONObject jsonobj = new JSONObject("{'name':'xiazdong','age':20}");
String name = jsonobj.getString("name");
int age = jsonobj.getInt("age");
System.out.println(name+":"+age);
}
}
package com.xiazdong.json;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
public class Test {
public static void main(String[] args) throws Exception{
JSONObject jsonobj = new JSONObject("{'name':'xiazdong','age':20}");
String name = jsonobj.getString("name");
int age = jsonobj.getInt("age");
System.out.println(name+":"+age);
}
}
2.JSONArray代码实例
view plaincopy to clipboardprint?package com.xiazdong.json;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
public class Test {
public static void main(String[] args) throws Exception{
JSONArray jsonarray = new JSONArray("[{'name':'xiazdong','age':20},{'name':'xzdong','age':15}]");
for(int i=0;i<jsonarray.length();i++){
String name = jsonarray.getJSONObject(i).getString("name");
int age = jsonarray.getJSONObject(i).getInt("age");
System.out.println("name="+name);
System.out.println("age="+age);
}
}
}
package com.xiazdong.json;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
public class Test {
public static void main(String[] args) throws Exception{
JSONArray jsonarray = new JSONArray("[{'name':'xiazdong','age':20},{'name':'xzdong','age':15}]");
for(int i=0;i<jsonarray.length();i++){
String name = jsonarray.getJSONObject(i).getString("name");
int age = jsonarray.getJSONObject(i).getInt("age");
System.out.println("name="+name);
System.out.println("age="+age);
}
}
}
3.嵌套JSONObject和JSONArray代码实例
view plaincopy to clipboardprint?package com.xiazdong.json;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
public class Test {
public static void main(String[] args) throws Exception{
String str = "{'name':'xiazdong','age':20,'book':['book1','book2']";
JSONObject obj = new JSONObject(str);
System.out.println(obj.getJSONArray("book").getString(0));
}
}
package com.xiazdong.json;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
public class Test {
public static void main(String[] args) throws Exception{
String str = "{'name':'xiazdong','age':20,'book':['book1','book2']";
JSONObject obj = new JSONObject(str);
System.out.println(obj.getJSONArray("book").getString(0));
}
}
4.JSONStringer代码示例
JSONStringer可以用来快速构建一个JSON格式的文本,并转换成String,可以写入文件;
JSONStringer是JSONWriter的子类;
JSONStringer一般通过object().key().value().key().value().endObject()进行构造;
object()表明开始一个对象,即添加{ ;
endObject()表明结束一个对象,即添加 } ;
array()表明开始一个数组,即添加一个 [ ;
endArray()表明结束一个数组,即添加一个 ] ;
key()表示添加一个key;
value()表示添加一个value;
view plaincopy to clipboardprint?package com.xiazdong.json;
import weibo4j.org.json.JSONStringer;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONStringer stringer = new JSONStringer();
String str = stringer.object().key("name").value("xiazdong").key("age").value(20).endObject().toString();
System.out.println(str);
}
}
package com.xiazdong.json;
import weibo4j.org.json.JSONStringer;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONStringer stringer = new JSONStringer();
String str = stringer.object().key("name").value("xiazdong").key("age").value(20).endObject().toString();
System.out.println(str);
}
}
复杂JSON格式写入
view plaincopy to clipboardprint?package com.xiazdong.json;
import java.io.File;
import java.io.FileReader;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
import weibo4j.org.json.JSONStringer;
import weibo4j.org.json.JSONTokener;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONStringer js = new JSONStringer();
JSONObject obj2 = new JSONObject();
JSONObject obj3 = new JSONObject();
JSONObject obj4 = new JSONObject();
obj4.put("title", "book1").put("price", "$11");
obj3.put("book", obj4);
obj3.put("author", new JSONObject().put("name", "author-1"));
JSONObject obj5 = new JSONObject();
JSONObject obj6 = new JSONObject();
obj6.put("title", "book2").put("price", "$22");
obj5.put("book", obj6);
obj5.put("author", new JSONObject().put("name", "author-2"));
JSONArray obj7 = new JSONArray();
obj7.put(obj3).put(obj5);
obj2.put("title","BOOK");
obj2.put("signing", obj7);
js.object().key("session").value(obj2).endObject();
System.out.println(js.toString());
PrintWriter out = new PrintWriter(new FileOutputStream(1.txt));
out.println(js.toString());
}
}
package com.xiazdong.json;
import java.io.File;
import java.io.FileReader;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONObject;
import weibo4j.org.json.JSONStringer;
import weibo4j.org.json.JSONTokener;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONStringer js = new JSONStringer();
JSONObject obj2 = new JSONObject();
JSONObject obj3 = new JSONObject();
JSONObject obj4 = new JSONObject();
obj4.put("title", "book1").put("price", "$11");
obj3.put("book", obj4);
obj3.put("author", new JSONObject().put("name", "author-1"));
JSONObject obj5 = new JSONObject();
JSONObject obj6 = new JSONObject();
obj6.put("title", "book2").put("price", "$22");
obj5.put("book", obj6);
obj5.put("author", new JSONObject().put("name", "author-2"));
JSONArray obj7 = new JSONArray();
obj7.put(obj3).put(obj5);
obj2.put("title","BOOK");
obj2.put("signing", obj7);
js.object().key("session").value(obj2).endObject();
System.out.println(js.toString());
PrintWriter out = new PrintWriter(new FileOutputStream(1.txt));
out.println(js.toString());
}
}
以上代码生成了如下JSON格式:
view plaincopy to clipboardprint?{"section":{
"title":"BOOK",
"signing":[
{
"author":
{
"name":"author-1"
},
"book":
{
"title":"book1",
"price":"$11"
}
},
{
"author":
{
"name":"author-2"
},
"book":
{
"title":"book2",
"price":"$22"
}
}
]}
}
}
{"section":{
"title":"BOOK",
"signing":[
{
"author":
{
"name":"author-1"
},
"book":
{
"title":"book1",
"price":"$11"
}
},
{
"author":
{
"name":"author-2"
},
"book":
{
"title":"book2",
"price":"$22"
}
}
]}
}
}
5.JSONTokener代码示例
JSONTokener是用来读取JSON格式的文件;
JSONObject obj = new JSONObject( new JSONTokener(java.io.Reader));可以从文件中读取一个JSONObject;
JSONArray obj = new JSONArray( new JSONTokener(java.io.Reader)); 可以从文件中读取一个JSONArray;
1.txt
view plaincopy to clipboardprint?{
'name':'xiazdong',
'book':['book1','book2']
}
{
'name':'xiazdong',
'book':['book1','book2']
}
view plaincopy to clipboardprint?package com.xiazdong.json;
import java.io.File;
import java.io.FileReader;
import weibo4j.org.json.JSONObject;
import weibo4j.org.json.JSONTokener;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONObject obj = new JSONObject(new JSONTokener(new FileReader(new File("1.txt"))));
System.out.println(obj.getJSONArray("book").getString(1)); //可以读取book2
}
}
package com.xiazdong.json;
import java.io.File;
import java.io.FileReader;
import weibo4j.org.json.JSONObject;
import weibo4j.org.json.JSONTokener;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONObject obj = new JSONObject(new JSONTokener(new FileReader(new File("1.txt"))));
System.out.println(obj.getJSONArray("book").getString(1)); //可以读取book2
}
}
复杂JSON格式的读取代码:
1.txt
view plaincopy to clipboardprint?{"section":{
"title":"BOOK",
"signing":[
{
"author":
{
"name":"author-1"
},
"book":
{
"title":"book1",
"price":"$11"
}
},
{
"author":
{
"name":"author-2"
},
"book":
{
"title":"book2",
"price":"$22"
}
}
]}
}
}
{"section":{
"title":"BOOK",
"signing":[
{
"author":
{
"name":"author-1"
},
"book":
{
"title":"book1",
"price":"$11"
}
},
{
"author":
{
"name":"author-2"
},
"book":
{
"title":"book2",
"price":"$22"
}
}
]}
}
}
view plaincopy to clipboardprint?package com.xiazdong.json;
import java.io.File;
import java.io.FileReader;
import weibo4j.org.json.JSONObject;
import weibo4j.org.json.JSONTokener;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONObject obj = new JSONObject(new JSONTokener(new FileReader(new File("1.txt"))));
System.out.println(obj.getJSONObject("section").getJSONArray("signing").getJSONObject(0).getJSONObject("author").getString("name")); <span style="white-space:pre"> </span>//获取author-1
}
}
package com.xiazdong.json;
import java.io.File;
import java.io.FileReader;
import weibo4j.org.json.JSONObject;
import weibo4j.org.json.JSONTokener;
public class JsonStringerDemo {
public static void main(String[] args) throws Exception{
JSONObject obj = new JSONObject(new JSONTokener(new FileReader(new File("1.txt"))));
System.out.println(obj.getJSONObject("section").getJSONArray("signing").getJSONObject(0).getJSONObject("author").getString("name")); <span style="white-space:pre"> </span>//获取author-1
}
}
总结:
1在Java中JSON格式的String最好用单引号表示;
2.利用JSONObject+JSONTokener能够读取JSON格式文件对象;
3.利用PrintWriter+JSONStringer可以写入JSON文件;
注:由于原本想要试图用JSONWriter写入,但是没有成功;所以只能利用JSONStringer+PrintWriter写入;