I have this restful web service http://firstsw.besaba.com/get_all.php?tab=doctors&cond=doc_id=2, I tested it with Advanced Rest Client plugin for Chrome and it work well. I want to parse the Json response with java code, so my code is:
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.*;
public class JsonArray {
public JsonArray() {
initJson();
}
public void initJson() {
URL url;
try {
url = new URL("http://firstsw.besaba.com/get_all.php?tab=doctors&cond=doc_id=2");
JSONObject obj = new JSONObject(url);
String success = obj.getString("success");
System.out.println(success+"/n");
JSONArray arr = obj.getJSONArray("element");
for(int i=0;i<att.length;i++){
String doc_id = arr.getJSONObject(i).getString("doc_id");
String doc_firstname = arr.getJSONObject(i).getString("doc_firstname");
String doc_lastname = arr.getJSONObject(i).getString("doc_lastname");
System.out.println("doc_id: "+doc_id+"/n"+"doc_firstname:"+doc_firstname+"/n"+"doc_lastname: "+doc_lastname);
}
} catch (MalformedURLException ex) {
Logger.getLogger(JsonArray.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
But I get those exceptions:
Exception in thread "main" org.json.JSONException: JSONObject["success"] not found.
Exception in thread "main" org.json.JSONException: JSONObject["element"] not found.
Objectas it makes novices think they can pass something likeURLthere.