String转JSONObject,并遍历JSONObject中的每个值

	/**
	 * 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 iterator = jsonObject.keys();
	    while(iterator.hasNext()){
	    	String key = iterator.next();
	    	String value = jsonObject.getString(key);
	    	System.out.println("遍历JSONObject对象中的key和value:"+key + "," + value);
	    }
	}

运行结果:
String转JSONObject,并遍历JSONObject中的每个值_第1张图片

你可能感兴趣的:(json)