package com.javasky.action;
import org.json.JSONArray;
import org.json.JSONObject;
public class JOSNDemo {
public static void main(String[] args) throws Exception{
String json = "{'hello':'world','abc':'xyz'}";
JSONObject jsonObject = new JSONObject(json);
String v1 = jsonObject.getString("hello");
String v2 = jsonObject.getString("abc");
System.out.println(v1);
System.out.println(v2);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
json = "[{'hello':'www','abc':false}, {'hello':'444','abc':true}]";
JSONArray jsonArry = new JSONArray(json);
for(int i = 0; i<jsonArry.length(); i++){
JSONObject jsObject2 = jsonArry.getJSONObject(i);
String m1 = jsObject2.getString("hello");
boolean m2 = jsObject2.getBoolean("abc");
System.out.println(m1);
System.out.println(m2);
}
}
}
显示出:
world
xyz
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
www
false
444
true