JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言。
以下展示三种处理json的java类库:
自定义的转换对象:
public class Element implements Serializable {
private String groupId;
private String artifactId;
private String version;
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getArtifactId() {
return artifactId;
}
public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String toString() {
return "GroupId: " + groupId + "\nArtifactId: " + artifactId
+ "\nVersion: " + version;
}
}
Gson:
public class GsonUtil {
public static String bean2Json(Object obj) {
Gson gson = new GsonBuilder().create();
return gson.toJson(obj);
}
public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
Gson gson = new GsonBuilder().create();
return gson.fromJson(jsonStr, objClass);
}
/**
* 把混乱的json字符串整理成缩进的json字符串
*
* @param uglyJsonStr
* @return
*/
public static String jsonFormatter(String uglyJsonStr) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJsonStr);
String prettyJsonString = gson.toJson(je);
return prettyJsonString;
}
}
Jaskon:(最快?)
public class JaskonUtil {
public static String bean2Json(Object obj) throws IOException {
ObjectMapper mapper = new ObjectMapper();
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, obj);
gen.close();
return sw.toString();
}
public static <T> T json2Bean(String jsonStr, Class<T> objClass)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonStr, objClass);
}
}
FastJson:
public class FastJsonUtil {
public static String bean2Json(Object obj) {
return JSON.toJSONString(obj);
}
public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
return JSON.parseObject(jsonStr, objClass);
}
}
主程序调用:
public class MainActivity extends Activity implements OnClickListener {
/**
*
<Element> <groupId>org.codehaus.jackson</groupId>
* <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version>
* </Element>
*/
private final static String testString = "{\"artifactId\":\"jackson-mapper-asl\",\"groupId\":\"org.codehaus.jackson\",\"version\":\"1.9.13\"}";
private Button btnGson;
private Button btnJackson;
private Button btnFastjson;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView) findViewById(R.id.result);
btnGson = (Button) findViewById(R.id.gson);
btnJackson = (Button) findViewById(R.id.jackson);
btnFastjson = (Button) findViewById(R.id.fastjson);
btnGson.setOnClickListener(this);
btnJackson.setOnClickListener(this);
btnFastjson.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.gson:
// json转对象
Element element = GsonUtil.json2Bean(testString, Element.class);
tvResult.setText(element.toString());
// ------------------------------------------
// 对象转json
/*
Element element = new Element();
element.setGroupId("testGroupId");
element.setArtifactId("testAId");
element.setVersion("1.1.1");
String res = GsonUtil.bean2Json(element);
Log.i("MainActivity", res);
tvResult.setText(res);
*/
// ------------------------------------------
// 格式化
/*
String res = GsonUtil.jsonFormatter(testString);
tvResult.setText(res);
*/
break;
case R.id.jackson:
// json转对象
try {
element = JaskonUtil.json2Bean(testString, Element.class);
tvResult.setText(element.toString());
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ------------------------------------------
// 对象转json
/*
element = new Element();
element.setGroupId("testGroupId");
element.setArtifactId("testAId");
element.setVersion("1.1.1");
String res;
try {
res = JaskonUtil.bean2Json(element);
tvResult.setText(res);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
break;
case R.id.fastjson:
// json转对象
element = FastJsonUtil.json2Bean(testString, Element.class);
tvResult.setText(element.toString());
// ------------------------------------------
// 对象转json
/*
element = new Element();
element.setGroupId("testGroupId");
element.setArtifactId("testAId");
element.setVersion("1.1.1");
String res = FastJsonUtil.bean2Json(element);
tvResult.setText(res);
*/
break;
default:
break;
}
}
}
参考:http://www.cnblogs.com/windlaughing/p/3241776.html