只得到JSON對象的第一個對象這是我的JSON對象: -從JSON字符串
{"emp_remark":"right now busy.. please wait",
"emp_loginid":"pra",
"emp_name":"Pragya Patel",
"emp_timein":"18:9",
"emp_id":"1",
"emp_timeout":"18:9",
"emp_emailid":"hsjsnejw",
"emp_mobno":"7879467946",
"emp_desigantion":"Android",
"emp_deviceid":"APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs"
}
"emp_remark":"right now busy.. please wait",
"emp_loginid":"deepp",
"emp_name":"deepak",
"emp_timein":"18:18",
"emp_id":"2",
"emp_timeout":"18:9",
"emp_emailid":"[email protected]",
"emp_mobno":"7469467946",
"emp_desigantion":"java",
"emp_deviceid":null
}
{
"emp_remark":"right now busy.. please wait",
"emp_loginid":"amu",
"emp_name":"amul",
"emp_timein":"18:18",
"emp_id":"3",
"emp_timeout":"18:9",
"emp_emailid":"[email protected]",
"emp_mobno":"7469462946",
"emp_desigantion":"java",
"emp_deviceid":"APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs"
}
現在在Android中,我在一個字符串獲取整個對象..但是當我轉換成JSON對象..它是隻得到了第一個目標,即
{
"emp_remark":"right now busy.. please wait",
"emp_loginid":"pra",
"emp_name":"Pragya Patel",
"emp_timein":"18:9",
"emp_id":"1",
"emp_timeout":"18:9",
"emp_emailid":"hsjsnejw",
"emp_mobno":"7879467946",
"emp_desigantion":"Android",
"emp_deviceid":"APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs"}
這裏是我的代碼:
@Override
protected Void doInBackground(Void... urls) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
Log.d(TAG, "Url : "+getUrl);
// Making a request to url and getting response
jsonStr = sh.makeServiceCall(getUrl, ServiceHandler.GET);
Log.d(TAG, "String json :" + jsonStr);
try {
employeeDetailsModel = JsonParsing.getEmployeePojo(jsonStr);
List<EmployeeDetailsModel> employeedesignationList = jsonParsing.getEmployeeDes(jsonStr);
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.d(TAG, "Exception: in parsing >> " + e.getMessage());
}
return null;
}
現在我另一個階級即Ĵ sonParsing
public static EmployeeDetailsModel getEmployeePojo(String jsonString) throws JSONException{
EmployeeDetailsModel employeeDetailsModel = null;
JSONObject jsonObject;
Log.w(TAG, "String json :" + jsonString);
if (jsonString != null) {
jsonObject = new JSONObject(jsonString);
}
Log.w(TAG, "Object json :" + jsonObject);
List<EmployeeDetailsModel> detailsModelList = new ArrayList<EmployeeDetailsModel>();
if(jsonObject != null && jsonObject.length() > 0){
employeeDetailsModel = new EmployeeDetailsModel();
employeeDetailsModel.setEmpId(jsonObject.getInt("emp_id"));
employeeDetailsModel.setEmpLoginId(jsonObject.getString("emp_loginid"));
employeeDetailsModel.setEmpName(jsonObject.getString("emp_name"));
employeeDetailsModel.setEmpMobNo(jsonObject.getString("emp_mobno"));
employeeDetailsModel.setEmpEmailId(jsonObject.getString("emp_emailid"));
employeeDetailsModel.setEmpTimeIn(jsonObject.getString("emp_timein"));
employeeDetailsModel.setEmpTimeOut(jsonObject.getString("emp_timeout"));
employeeDetailsModel.setEmpdeviceId(jsonObject.getString("emp_deviceid"));
employeeDetailsModel.setEmpDes(jsonObject.getString("emp_desigantion"));
employeeDetailsModel.setEmpDes(jsonObject.getString("emp_remark"));
detailsModelList.add(employeeDetailsModel);
for(EmployeeDetailsModel detailsModel : detailsModelList){
Log.w(TAG, "EMP ID : " + employeeDetailsModel.getEmpId() + " EMP Name : " + employeeDetailsModel.getEmpName());
Log.w(TAG, "EMP DEVICE ID : " + employeeDetailsModel.getEmpdeviceId());
}
}
return employeeDetailsModel;
}
,這是我的logcat
5720-15608/com.gatepass D/GateEntryFormActivity: String json :{"emp_remark":"right now busy.. please wait","emp_loginid":"pra","emp_name":"Pragya Patel","emp_timein":"18:9","emp_id":"1","emp_timeout":"18:9","emp_emailid":"hsjsnejw","emp_mobno":"7879467946","emp_desigantion":"Android","emp_deviceid":"APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs"}{"emp_remark":"right now busy.. please wait","emp_loginid":"deepp","emp_name":"deepak","emp_timein":"18:18","emp_id":"2","emp_timeout":"18:9","emp_emailid":"[email protected]","emp_mobno":"7469467946","emp_desigantion":"java","emp_deviceid":null}{"emp_remark":"right now busy.. please wait","emp_loginid":"amu","emp_name":"amul","emp_timein":"18:18","emp_id":"3","emp_timeout":"18:9","emp_emailid":"[email protected]","emp_mobno":"7469462946","emp_desigantion":"java","emp_deviceid":"APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs"}
07-20 11:32:45.371 5720-15608/com.gatepass W/JSON PARSING CLASSS: Object json :{"emp_remark":"right now busy.. please wait","emp_loginid":"pra","emp_name":"Pragya Patel","emp_id":"1","emp_timein":"18:9","emp_timeout":"18:9","emp_mobno":"7879467946","emp_emailid":"hsjsnejw","emp_desigantion":"Android","emp_deviceid":"APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs"}
07-20 11:32:45.371 5720-15608/com.gatepass W/JSON PARSING CLASSS: EMP ID : 1 EMP Name : Pragya Patel
07-20 11:32:45.371 5720-15608/com.gatepass W/JSON PARSING CLASSS: EMP DEVICE ID : APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs
在JSON對象,只得到第一個對象
JSON是無效的,如果你想把幾個對象放在一起,你必須把它們分開放在一個數組中。 – Yaron
瀏覽JSON數據格式無效..請將cheak –
@Pragya Patel使用[this](http://pro.jsonlint.com/)檢查您的json – Nisarg