2016-07-20 113 views
-1

只得到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對象,只得到第一個對象

+3

JSON是無效的,如果你想把幾個對象放在一起,你必須把它們分開放在一個數組中。 – Yaron

+0

瀏覽JSON數據格式無效..請將cheak –

+0

@Pragya Patel使用[this](http://pro.jsonlint.com/)檢查您的json – Nisarg

回答

0

這是一個畸形的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" } ]

+0

請告訴我如何使用Java獲取正確格式的json –

+0

您需要更改從服務器獲取的JSON。 Java代碼中沒有任何東西。 – Anandroid

0

轉換Json中的字符串陣列。

JSONArray jsonArray = new JSONArray(str); 

然後使用下面的代碼。

JsonObject jsonObject; 
for (int i = 0; i < jsonArray.length(); i++) { 
      jsonObject = jsonArray.getJSONObject(i); 
    } 
0

我覺得這不是一個正常的jsonList像[{A: 「1」,B: 「2」},{C: 「3」 d: 「4」}],所以你只能得到第一個對象。 對象是這樣的:{key,value} 這樣的列表:[collection,collection]

+0

請在自己的答案中加入儘可能多的信息,不建議使用外部鏈接,因爲它們可以在以後被破解或可以更改內容。 –