json-smart 使用示例
关于 json 库,请使用 fastjson,这是我用过的最好用的 json 库!
=======================================================
json 是一种通用的数据格式。相比与 protocal buffer、thrift 等数据格式,json 具有可读性强(文本)、天生具备良好的扩展性(随意增减字段)等优良特点,利用 json 作为通讯协议,开发效率更高。当然相较于二进制协
议,文本更耗带宽。
json 和 HTTP 协议都是基于文本的,天生的一对。面对多终端的未来,使用 Json 和 HTTP 作为前端架构的基础将成为开发趋势。
简介
json-smart 官方主页
特性
性能评测 Json-smart-API:
javadoc:
使用示例
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONStyle;
import net.minidev.json.parser.ParseException;
import java.io.UnsupportedEncodingException;
import java.util.*;
/*
* Home page: http://code.google.com/p/json-smart/
*
* compiler: javac -cp json-smart-1.1.1.jar JsonSmartTest.java
*
* Run: java -cp ./:json-smart-1.1.1.jar JsonSmartTest
*
*/
public class JsonSmartTest {
//1. String <==> JsonObject
public static void DecodingTest() throws ParseException {
System.out.println("=======decode=======");
String s="[0,{'1':{'2':{'3':{'4':[5,{'6':7}]}}}}]";
Object obj=JSONValue.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=JSONValue.parse(s);
System.out.println(obj);
s="{\"key\":\"Value\"}";
// JSONValue.parseStrict()
// can be use to be sure that the input is wellformed
obj=JSONValue.parseStrict(s);
JSONObject obj3=(JSONObject)obj;
System.out.println("====== Object content ======");
System.out.println(obj3.get("key"));
System.out.println();
评论0
最新资源