最近项目中需要做数据在Android端与服务器之间的传输。所以用到了fastjson,将服务器的数据转换成json格式后发送到Android端,然后在Android端解析json数据。
参考了网上的很多做法和自己尝试之后,总结出一些简单的使用方法如下:
fastjson官方链接:
fastjson官方链接
json数据的构建:
public static void creatJsonStr(){
JSONObject obj = new JSONObject();
obj.put("name", "Lucky");
obj.put("age", 19);
// 子对象
JSONObject objContact = new JSONObject();
objContact.put("tel", "18987654321");
objContact.put("email", "lucky@666.com");
obj.put("contact", objContact);
// 子数组对象
JSONArray scoreArr = new JSONArray();
JSONObject objEnglish = new JSONObject();
objEnglish.put("course", "chainese");
objEnglish.put("result", 100);
objEnglish.put("level", "A");
JSONObject objMath = new JSONObject();
objMath.put("course", "math");
objMath.put("result", 50);
objMath.put("level", "D");
scoreArr.add(objEnglish);
scoreArr.add(objMath);
obj.put("score", scoreArr);
System.err.println(obj.toString());
}
运行结果如下:
为什么要选用这个例子呢?
上面的这个例子包括:
如何构建总的json对象,如何添加多个子对象,如何构建对象组等。非常全面,是一个很好的学习fastjson用法的例子。
————————————————
原文链接:https://blog.csdn.net/shasiqq/article/details/80239972
在Android端进行json数据的解析: