json-lib提供了许多org.json没有的功能,也就是对org.json的拓展,它需要很多的jar包,这里不多讲。
org.json是一个轻量级的java json处理工具,android sdk集成了org.json,所以我选择使用org.json,这篇博文也是将org.json。
关于org.json,官网www.json.org没有直接提供org.json的jar包,只提供了java源码。
先在官网下载java源码,下载源码链接会转到github:
下载这个项目,获得这些源码,用eclipse制作json.jar
这里直接分享我制作的json.jar
json.zip有兴趣的同学可以先看看源码。
先看看json.org.cn对org.json的描述

对于这几个重要的类,下面直接在代码中介绍。
实例讲解
-
package com.vibexie.orgJson;
-
-
import java.io.BufferedReader;
-
import java.io.BufferedWriter;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.InputStreamReader;
-
import java.io.OutputStream;
-
import java.io.OutputStreamWriter;
-
-
import org.json.JSONArray;
-
import org.json.JSONObject;
-
import org.json.JSONStringer;
-
import org.json.JSONTokener;
-
import org.json.JSONWriter;
-
/**
-
* 测试的是org.json,不是json-lib
-
* Title : Test.java
-
* Company: ZhenBot
-
* Author : Vibe Xie @
-
* Time : Apr 20, 2015 8:25:54 AM
-
* Copyright: Copyright (c) 2015
-
* Description:
-
*/
-
public class Test {
-
-
public static void main(String[] args) {
-
System.out.println("------------------使用JSONObject and JSONArray构造和解析json-----------------");
-
/**
-
* JSONObject,构建json对象
-
*/
-
JSONObject jsonObject=new JSONObject();
-
jsonObject.put("name", "VibeXie");
-
jsonObject.put("phone", "18888888888");
-
-
/**
-
* JSONArray,构建json数组
-
*
-
*/
-
JSONArray jsonArray=new JSONArray();
-
jsonArray.put("vibexie@qq.com").put("809403300@qq.com");
-
/**
-
* 将jsonArray插入到jsonObject中
-
*/
-
jsonObject.put("email", jsonArray);
-
-
System.out.println("构建json...");
-
System.out.println(jsonObject);
-
-
/**
-
* 解析json
-
*/
-
System.out.println("解析json...");
-
System.out.println("name:"+jsonObject.get("name"));
-
System.out.println("phone:"+jsonObject.get("phone"));
-
-
JSONArray emailArray=jsonObject.getJSONArray("email");
-
/**
-
* 注意获取JSONArray的index是从0开始的。
-
*/
-
System.out.println("email:"+emailArray.getString(0)+","+emailArray.getString(1));
-
-
-
-
System.out.println("\n\n------------------使用JSONStringer构造json-----------------");
-
-
/**
-
* JSONStringer是一个构建json的工具
-
*/
-
JSONStringer jsonStringer=new JSONStringer();
-
/**
-
* 注意一定要开头调用.object()结尾调用.endObject()方法。
-
*/
-
jsonStringer.object()
-
.key("name")
-
.value("VibeXie")
-
.key("phone")
-
.value("18888888888")
-
.key("email")
-
.value(jsonArray)
-
.endObject();
-
System.out.println("构建json...");
-
System.out.println(jsonStringer);
-
-
System.out.println("解析json...");
-
System.out.println("name:"+jsonObject.get("name"));
-
System.out.println("phone:"+jsonObject.get("phone"));
-
System.out.println("email:"+emailArray.getString(0)+","+emailArray.getString(1));
-
-
System.out.println("\n\n------------------使用JSONTokener解析json字符串-----------------");
-
/**
-
* JSONTokener读取源字符串, 并解析成token. 它被JSONObject和JSONArray使用来解析JSON字符串.
-
*/
-
-
System.out.println("使用JSONObject.nextValue...");
-
JSONTokener jsonTokener1=new JSONTokener(jsonObject.toString());
-
JSONObject jsonObject1=(JSONObject)jsonTokener1.nextValue();
-
System.out.println("构建json...");
-
System.out.println(jsonObject1);
-
-
System.out.println("\n使用JSONObject.nextValue...");
-
JSONTokener jsonTokener2=new JSONTokener(jsonObject.toString());
-
/**
-
* 截取当前位置到之后第一个'n'字符之间的字符串,这里的当前位置是起始位置0
-
*/
-
System.out.println(jsonTokener2.nextString('n'));
-
/**
-
* 截取当前位置到之后第一个'8'字符之间的字符串,这里的当前位置是前面'n'字符
-
*/
-
System.out.println(jsonTokener2.nextString('8'));
-
/**
-
* 这里nextValue()获取'8'后面的一个字符串,也就是"88888888",这样你就明白nextValue()的作用了
-
*/
-
System.out.println(jsonTokener2.nextValue());
-
/**
-
* 再获取下一个一个字符串,也就是"88888888"后面的",",注意","也是下一个value,它是一个字符串
-
*/
-
System.out.println(jsonTokener2.nextValue());
-
-
/**
-
* 关于JSONTokener,不再测试其它方法了,要理解这就是一个解析json字符串的一个工具,通过移动字符串当前字符位置来解析字符串
-
*/
-
-
-
System.out.println("\n\n------------------使用JSONWriter将json写入io流或者从io流读取-----------------");
-
JSONWriter jsonWriter=null;
-
BufferedWriter bufferedWriter=null;
-
BufferedReader bufferedReader=null;
-
try {
-
/**
-
* 将json保存到文件/home/vibexie/test.json
-
*/
-
OutputStream outputStream=new FileOutputStream(new File("/home/vibexie/test.json"));
-
bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream));
-
jsonWriter = new JSONWriter(bufferedWriter);
-
System.out.println("将json写入到文件/home/vibexie/test.json...");
-
/**
-
* 注意,调用endObject()默认调用BufferedWriter.write()方法写入io流
-
*/
-
jsonWriter.object()
-
.key("name")
-
.value("VibeXie")
-
.key("phone")
-
.value("18888888888")
-
.key("email")
-
.value(jsonArray)
-
.endObject();
-
/**
-
* flush必须调用
-
*/
-
bufferedWriter.flush();
-
-
/**
-
* 从文件/home/vibexie/test.json读取json
-
*/
-
InputStream inputStream=new FileInputStream(new File("/home/vibexie/test.txt"));
-
bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
-
System.out.println("从文件/home/vibexie/test.json读取json...");
-
System.out.println(bufferedReader.readLine());
-
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}finally{
-
try {
-
/**
-
* 关闭io
-
*/
-
bufferedWriter.close();
-
bufferedReader.close();
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
- }