需要jar包:json-lib-2.4-jdk15.jar
一、
String str = "{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}";
此时用JSONObject:
1 String str = "{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}"; 2 JSONObject json = JSONObject.fromObject(str); 3 System.out.println(json.getString("name"));
二、
String jsonString = "[{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}"
+ ",{\"name\":\"lisi\",\"password\":\"lisi123\",\"email\":\"1435123@qq.com\"}]";
此时用JSONArray:
String jsonString = "[{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}"
+ ",{\"name\":\"lisi\",\"password\":\"lisi123\",\"email\":\"1435123@qq.com\"}]";
JSONArray jsonArray = JSONArray.fromObject(jsonString);
for(int i = 0 ;i < jsonArray.size() ; i++){
System.out.println("name = "+jsonArray.getJSONObject(i).getString("name"));
System.out.println("password = "+jsonArray.getJSONObject(i).getString("password"));
System.out.println("email = "+jsonArray.getJSONObject(i).getString("email"));
}
如果jar包是 com.alibaba.fastjson.JSONArray;
第二个的方法可能是 JSONArray jsonArray = JSONArray.parseArray(jsonString );