/**
* String转JSONObject
*/
public static void test2(){
String json = "{\"name\":\"张三\",\"code\":\"123\"}";
JSONObject jsonObject = new JSONObject(json);
System.out.println("String转JSONObject:"+jsonObject);
System.out.println();
}
/**
* 遍历JSONObject
* JSONObject 含义:object对象是json格式的
*/
public static void test4(){
String json = "{\"name\":\"张三\",\"code\":\"123\"}";
JSONObject jsonObject = new JSONObject(json);
Iterator<String> iterator = jsonObject.keys();
while(iterator.hasNext()){
String key = iterator.next();
String value = jsonObject.getString(key);
System.out.println("遍历JSONObject对象中的key和value:"+key + "," + value);
}
}
运行结果: