Reputation: 23
I'm new in Android and JSON. Curently trying to fetch my data out from database. But it shows error. This is my logcat:
I/mytag: my json string{"success":1,"message":"Lecturer's Available","lecturer":[{"lecName":"Ariah","lecID":"1234","lecEmail":"ariah@gmail.com","lecPass":"123"}]}
W/System.err: org.json.JSONException: No value for lecName
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
W/System.err: at com.example.foo.isfoo.lec_profile$LecturerProfile.doInBackground(lec_profile.java:99)
W/System.err: at com.example.foo.isfoo.lec_profile$LecturerProfile.doInBackground(lec_profile.java:82)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
My question is how to get Json array? What I need to add for the error no value for lecName above? This is my java coding.
protected String doInBackground(String... args) {
try {
json=jsonParser.getJSONFromUrl(PROFILE_URL);
Log.i("mytag","my json string"+ json);
lec_Name=json.getString(TAG_NAME);
lec_ID=json.getString(TAG_ID);
lec_Email=json.getString(TAG_EMAIL);
lec_Pass=json.getString(TAG_PASSWORD);
} catch (JSONException e){
e.printStackTrace();
}
return null;
}
Really appreciate if someone can help me.
Upvotes: 0
Views: 823
Reputation: 1729
Go through this code.
protected String doInBackground(String... args) {
try {
json=jsonParser.getJSONFromUrl(PROFILE_URL);
JSONArray jsonArray = json.getJSONArray("lecturer");
for(int i = 0; i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String lec_Name = jsonObject.getString(TAG_NAME);
String lec_ID = jsonObject.getString(TAG_ID);
String lec_Email = jsonObject.getString(TAG_EMAIL);
String lec_Pass = jsonObject.getString(TAG_PASSWORD);
}
} catch (JSONException e){
e.printStackTrace();
}
return null;
}
Upvotes: 2
Reputation: 3422
Follow below code;
public static String[] ids;
public static String[] names;
public static String[] password;
public static String[] repassword;
JSONObject jsonObject = new JSONObject(strjson);
JSONArray users = jsonObject.getJSONArray("lecturer");
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(TAG_NAME);
names[i] = jo.getString(TAG_ID);
password[i] = jo.getString(TAG_PASSWORD);
}
Upvotes: 0