日期:2014-05-16 浏览次数:20347 次
{"url":"http://www.cnblogs.com/qianxudetianxia"}
JSONObject demoJson = new JSONObject(jsonString); String url = demoJson.getString("url");
{"name":"android","name":"iphone"}
JSONObject demoJson = new JSONObject(jsonString); String name = demoJson.getString("name"); String version = demoJson.getString("version"); System.out.println("name:"+name+",version:"+version);
{"number":[1,2,3]}
JSONObject demoJson = new JSONObject(jsonString); JSONArray numberList = demoJson.getJSONArray("number"); for(int i=0; i<numberList.length(); i++){ //因为数组中的类型为int,所以为getInt,其他getString,getLong同用 System.out.println(numberList.getInt(i)); }
{"number":[[1],[2],[3]]}
//嵌套数组遍历 JSONObject demoJson = new JSONObject(jsonString); JSONArray numberList = demoJson.getJSONArray("number"); for(int i=0; i<numberList.length(); i++){ //获取数组中的数组 System.out.println(numberList.getJSONArray(i).getInt(0)); }
{"mobile":[{"name":"android"},{"name":"iphone"}]}
JSONObject demoJson = new JSONObject(jsonString); JSONArray numberList = demoJson.getJSONArray("mobile"); for(int i=0; i<numberList.length(); i++){ System.out.println(numberList.getJSONObject(i).getString("name")); }
//无url节点,抛出异常 String url = demoJson.getString("url"); //无url节点,返回空,如果为基本类型,则返回默认值 String url = demoJson.optString("url");
String jsonString = getJsonString(); jsonString = jsonString.substring(j