关于json的小例子

来源:互联网 发布:网络服务器机柜厂家 编辑:程序博客网 时间:2024/06/11 10:04

今天用到了java  json  以前也有用到  就是没有写下来的好习惯


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;




public class Test {


public static void main(String[] args) {
String str="{'name':xiaoming;'age':20;'tongxue':[{'laoda':大菊},{'laoer':二局},{'laosan':三局},{'laosi':四局}]}";
try {
JSONObject json= new JSONObject(str); 
System.out.println(json.get("name"));
System.out.println("age");
JSONArray js  = json.getJSONArray("tongxue");
JSONObject bb= (JSONObject)js.get(0);
System.out.println(bb.get("laoda"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

很精髓 ,只需要这个json的jar包即可