首页 > 新闻动态 >  

新闻动态
NEWS

json-smart 应用示例

添加时间:2013-8-8 点击量:

json是一种通用的数据格局。比拟与protocal buffer、thrift等数据格局,json具有可读性强(文本)、生成具备杰出的扩大性(随便增减字段)等优良特点,哄骗json作为通信和谈,开辟效力更高。当然相较于二进制和谈,文本更耗带宽。


json和HTTP和谈都是基于文本的,生成的一对。面对多终端的将来,应用Json和HTTP作为前端架构的根蒂根基将成为开辟趋势。


简介


json-smart官方主页:https://code.google.com/p/json-smart/


特点:https://code.google.com/p/json-smart/wiki/FeaturesTests


机能评测:https://code.google.com/p/json-smart/wiki/Benchmark


Json-smart-API:  http://www.jarvana.com/jarvana/view/net/minidev/json-smart/1.0.9/json-smart-1.0.9-javadoc.jar!/net/minidev/json/package-summary.html


javadoc: https://github.com/u2waremanager/maven-repository/blob/master/net/minidev/json-smart/1.1.1/json-smart-1.1.1-javadoc.jar


应用示例



import net.minidev.json.JSONObject;

import net.minidev.json.JSONValue;
import net.minidev.json.JSONArray;
import net.minidev.json.JStyle;
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();

}

public static void EncodingTest() {
System.out.println(
=======EncodingTest=======);

// Json Object is an HashMap<String, Object> extends
JSONObject obj = new JSONObject();
obj.put(
name, foo);
obj.put(
num, 100);
obj.put(
balance, 1000.21);
obj.put(
is_vip, true);
obj.put(
nickname,null);

System.out.println(
Standard RFC4627 JSON);
System.out.println(obj.toJString());

System.out.println(
Compacted JSON Value);
System.out.println(obj.toJString(JStyle.MAX_COMPRESS));

// if obj is an common map you can use:

System.out.println(
Standard RFC4627 JSON);
System.out.println(JSONValue.toJString(obj));

System.out.println(
Compacted JSON Value);
System.out.println(JSONValue.toJString(obj, JStyle.MAX_COMPRESS));

}

public static void EncodingTest2() {
System.out.println(
=======EncodingTest2=======);

// Json Object is an HashMap<String, Object> extends
JSONObject obj = new JSONObject();
obj.put(
name, foo);
obj.put(
num, 100);
obj.put(
balance, 1000.21);
obj.put(
is_vip, true);
obj.put(
nickname,null);

//Output Compressed json
Object value = obj;
String com_json
= JSONValue.toJString(value, JStyle.MAX_COMPRESS);
String json
= JSONValue.toJString(value, JStyle.NO_COMPRESS);

System.out.println(
Compacted JSON Value);
System.out.println(com_json);
System.out.println(
From RFC4627 JSON String: + JSONValue.compress(json));
System.out.println(
From Compacted JSON String: + JSONValue.compress(com_json));

System.out.println(
Standard RFC4627 JSON Value);
System.out.println(json);
System.out.println(
From RFC4627 JSON String: + JSONValue.uncompress(json));
System.out.println(
From Compacted JSON String: + JSONValue.uncompress(com_json));

// compress json string
System.out.println(From compress json string(JSONObject));
Object obj2
=JSONValue.parse(com_json);
System.out.println(JSONValue.toJString(obj2, JStyle.NO_COMPRESS));
System.out.println(JSONValue.toJString(obj2, JStyle.MAX_COMPRESS));
}


//2. Java Struct <==> JsonSmart object
public class Person {
String name;
int age;
boolean single;
long mobile;

public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public boolean getSingle() {
return this.single;
}
public void setSingle(boolean single) {
this.single = single;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
}

public class JSONDomain { // for convert struct <==> json
public Object result = new JSONObject();

public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}

public void Struct2JsonObject() {
System.out.println(
========Struct2JsonObject=======);

Person person
= new Person();
person.setName(
json smart);
person.setAge(
13);
person.setMobile(
20130808);

Person person2
= new Person();
person2.setName(
test);
person2.setAge(
123);
person2.setMobile(
888666);

List
<Person> array = new ArrayList<Person>();
array.add(person);
array.add(person2);

//1. struct <==> JsonObject
JSONObject obj = new JSONObject();
//obj = (Object)person; // compiler error!
// way 1:
JSONDomain data = new JSONDomain(); // for convert
data.setResult(person);
// obj = (JSONObject)data.getResult(); // run error: ClassCastException
obj.put(person, data.getResult());
System.out.println(JSONValue.toJString(obj));

// way 2:
obj.put(person, array.get(1));
System.out.println(JSONValue.toJString(obj));


//2. Container <==> JsonObject
JSONArray jsonArray = new JSONArray();
jsonArray.add(person);
jsonArray.add(person2);
JSONObject result
= new JSONObject();
result.put(
persons, jsonArray);
System.out.println(JSONValue.toJString(result));
}

//3. JsonSmartSerializationTest
public static Map<String, Object> testBytes2Map(byte[] bytes) {
Map
<String, Object> map = null;
try {
map
= (Map<String, Object>) JSONValue.parseStrict((new String(bytes, UTF-8)));
}
catch (ParseException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return map;
}

// JsonSmartSerializationTest
public static byte[] testMap2Bytes(Map<String, Object> map) {
String str
= JSONObject.toJString(map);
byte[] result = null;
try {
result
= str.getBytes(UTF-8);
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}

public static void main(String[] args) throws Exception {
DecodingTest();

EncodingTest();

EncodingTest2();


JsonSmartTest test
= new JsonSmartTest();
test.Struct2JsonObject();

}
}


参考


Java序列化与JSON序列化拼


Java序列化与JSON序列化拼2


多终端的前端架构选择

原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
分享到: