java解析json,The constructor JSONObject(String) is undefined,错哪了啊??
///json-lib-2.4-jdk15.jar已导入importnet.sf.json.JSONArray;importnet.sf.json.JSONExcepti...
///json-lib-2.4-jdk15.jar已导入
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) throws JSONException {
String jsonString = "{\"people\":[{\"firstName\":\"Brett\",\"lastName\":\"McLaughlin\",\"email\":\"aaaa\"},{\"firstName\":\"Jason\",\"lastName\":\"Hunter\",\"email\":\"bbbb\"},{\"firstName\":\"Elliotte\",\"lastName\":\"Harold\",\"email\":\"cccc\"}]}";
JSONObject dataJson = new JSONObject(jsonString);
JSONArray array = dataJson.getJSONArray("people");
for (int i = 0; i < array.length(); i++) {
System.out.println("array:" + array.get(i));
System.out.println("firstName:" + array.getJSONObject(i).get("firstName"));
System.out.println("lastName:" + array.getJSONObject(i).get("lastName"));
System.out.println("email:" + array.getJSONObject(i).get("email"));
}
}
} 展开
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) throws JSONException {
String jsonString = "{\"people\":[{\"firstName\":\"Brett\",\"lastName\":\"McLaughlin\",\"email\":\"aaaa\"},{\"firstName\":\"Jason\",\"lastName\":\"Hunter\",\"email\":\"bbbb\"},{\"firstName\":\"Elliotte\",\"lastName\":\"Harold\",\"email\":\"cccc\"}]}";
JSONObject dataJson = new JSONObject(jsonString);
JSONArray array = dataJson.getJSONArray("people");
for (int i = 0; i < array.length(); i++) {
System.out.println("array:" + array.get(i));
System.out.println("firstName:" + array.getJSONObject(i).get("firstName"));
System.out.println("lastName:" + array.getJSONObject(i).get("lastName"));
System.out.println("email:" + array.getJSONObject(i).get("email"));
}
}
} 展开
2个回答
展开全部
import java.util.Iterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test6
{
public static void main(String[] args)
{
String str =
"{\"people\":[{\"firstName\":\"Brett\",\"lastName\":\"McLaughlin\",\"email\":\"aaaa\"},{\"firstName\":\"Jason\",\"lastName\":\"Hunter\",\"email\":\"bbbb\"},{\"firstName\":\"Elliotte\",\"lastName\":\"Harold\",\"email\":\"cccc\"}]}";
JSONObject jo = JSONObject.fromString(str);
JSONArray info = jo.getJSONArray("people");
System.out.println(info);
for(int i = 0; i < info.length(); i++)
{
JSONObject obj = info.getJSONObject(i);
Iterator it = obj.keys();
while(it.hasNext())
{
String key = (String) it.next();
System.out.println(obj.get(key));
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询