从json字符串

时间:2016-07-20 06:09:47

标签: java android 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":"deep@gmail.com",
"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":"amu@gmail.com",
"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;
    }

现在我是另一个类,即JsonParsing

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":"deep@gmail.com","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":"amu@gmail.com","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 个答案:

答案 0 :(得分: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": "deep@gmail.com", "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": "amu@gmail.com", "emp_mobno": "7469462946", "emp_desigantion": "java", "emp_deviceid": "APA91bFLNsLOz2iiSw9r2NKdlnWCWtNNNb-VTVY3TwmT7Nly88NnSJjJwoLNC3qveU7LSW9QY5t71JAejnkogvQRPXA-uEtnlg-1cve00k_4UhIinUH0Lzs" } ]

答案 1 :(得分:0)

转换JsonArray中的字符串。

JSONArray jsonArray = new JSONArray(str);

然后使用以下代码。

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

答案 2 :(得分:0)

我认为它不是普通的jsonList,如[{a:&#34; 1&#34;,b:&#34; 2&#34;},{c:&#34; 3&#34 ;,d:&#34; 4&#34;}],所以你只能得到第一个对象。 像这样的对象:{key,value} 像这样的列表:[集合,集合]