java解析json格式文件

来源:互联网 发布:同学通讯录查询软件 编辑:程序博客网 时间:2024/05/22 13:33

所用jar包,json-org.jar
 下载地址:http://www.docjar.com/jar_detail/json-org.jar.html


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


public class JsonUtil {
public static void main(String[] args) {
String str="{\"name\":\"putixiaoshifu\",\"skills\":{\"java\":[\"jsp\",\"js\"]},\"hobbies\":{\"static\":[{\"morning\":\"music\",\"noon\":\"tvShows\"}],\"dynamic\":\"sports\"}}";
 try {
JSONObject j = new JSONObject(str);
JSONObject jsonName = j.getJSONObject("skills");
System.out.println(jsonName);
JSONArray jsonArray = jsonName.getJSONArray("java");
System.out.println(jsonArray);
String a = (String) jsonArray.get(0);
System.out.println(a);
System.out.println("----------------------------------------------");
JSONObject hobbies = j.getJSONObject("hobbies");
System.out.println(hobbies);
JSONArray staticJson = hobbies.getJSONArray("static");
System.out.println(staticJson);
JSONObject today = (JSONObject) staticJson.get(0);
System.out.println("today"+today);
String mo  = (String) today.get("morning");
System.out.println(mo);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
0 0
原创粉丝点击