[英]How to parse json data using volley
I am using volley lib. 我正在使用volley lib。 at every activities in my project my first login page has done using volley JSON Object Request method(POST) and next activities could not parsed.
在我的项目中的每个活动中,我的第一个登录页面都使用了齐射JSON对象请求方法(POST),并且无法解析下一个活动。
Json data of the main screen: 主屏幕的Json数据:
{
"err-code": 5,
"job_details": [
{
"job_id": 33,
"job_no": "ES53-AF",
"contract_manager_id": 4,
"company_name": "A Construction Ltd",
"time_spent": 4.5
},
{
"job_id": 5,
"job_no": "ES1465-AF",
"contract_manager_id": 4,
"company_name": "Trios Property",
"job_description": "Carry out the rewire of ",
"time_spent": 26.5
},
{
"job_id": 81,
"job_no": "ES101-AF",
"contract_manager_id": 4,
"company_name": "Arden Construction Ltd",
"job_description": "Carry out works as per esti 3AQ",
"time_spent": 2.5
},
}]
}
Code: 码:
private void getDataNew()
{
String url = "http://103.5.103.8:067/ivservices/Webservices/joblisting";
HashMap<String, String> user = session.getUserDetails();
String token = user.get(SessionManager.KEY_TOKEN);
Log.e("token check kro", token);
final String role = user.get(SessionManager.KEY_ROLE);
Log.e("role", role);
String user_id = user.get(SessionManager.KEY_USERID);
// Log.e("user_id", user_id.toString());
Map<String, String> params = new HashMap();
params.put(KEY_TOKEN, token);
params.put(KEY_ROLE, role);
params.put(KEY_ID, user_id);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Log.e("response mila", response.toString());
parseData(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
error.printStackTrace();
//TODO: handle failure
}
});
Volley.newRequestQueue(this).add(jsonRequest);
}
Try this 尝试这个
Note Check Also your json remove ,} before ] than you have without syntax error response
注意检查还有你的json remove,}之前没有语法错误响应
try {
int err_code=response.getInt("err-code");
JSONArray jsonArray=response.getJSONArray("job_details");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObjectdetail=jsonArray.getJSONObject(i);
int job_id=jsonObjectdetail.getInt("job_id");
int time_spent=jsonObjectdetail.getInt("time_spent");
int contract_manager_id=jsonObjectdetail.getInt("contract_manager_id");
String job_no=jsonObjectdetail.getString("job_no");
String company_name=jsonObjectdetail.getString("company_name");
}
} catch (JSONException e) {
e.printStackTrace();
}
remove Extra }, from your json. 从你的json中删除Extra}。
one mistake you did in json parsing code you replied to @Er. 你在json解析你回复@Er的代码时犯了一个错误。 Arjun saini .
Arjun saini。
that mistake is in this line : 这个错误在这一行:
JSONArray jsonArray = Object.getJSONArray(" err-code "); JSONArray jsonArray = Object.getJSONArray(“ err-code ”);
it should be like this : 它应该是这样的:
JSONArray jsonArray = Object.getJSONArray(" job_details "); JSONArray jsonArray = Object.getJSONArray(“ job_details ”);
Make sure json should be like this: 确保json应该是这样的:
{
"err-code": 5,
"job_details": [
{
"job_id": 33,
"job_no": "ES53-AF",
"contract_manager_id": 4,
"company_name": "A Construction Ltd",
"time_spent": 4.5
},
{
"job_id": 5,
"job_no": "ES1465-AF",
"contract_manager_id": 4,
"company_name": "Trios Property",
"job_description": "Carry out the rewire of ",
"time_spent": 26.5
},
{
"job_id": 81,
"job_no": "ES101-AF",
"contract_manager_id": 4,
"company_name": "Arden Construction Ltd",
"job_description": "Carry out works as per esti 3AQ",
"time_spent": 2.5
}
]
}
And Try this code hope it will work if not work than please share logcat errors and exceptions 并尝试此代码希望它将工作,如果不工作,请分享logcat错误和异常
try {
JSONObject Object=new JSONObject(response);
int code = Object.getInt("err-code");
if (code == 0) {
JSONArray jsonArray = Object.getJSONArray("job_details");
List<JobList> jobListArray=new ArrayList<JobList>();
for (int i = 0; i <= jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
JobList model_joblist=new JobList();
model_joblist.setJobno(jsonObject.getString(JobList.JOB_NO));
model_joblist.setJobid(jsonObject.getInt(JobList.JOB_ID));
model_joblist.setDescreption(jsonObject.getString(JobList.JOB_DESCRIPTION));
jobListArray.add(model_joblist);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if it still not work than there is error in any other code eg in json are in your model_joblist etc. 如果它仍然不起作用,那么任何其他代码中都有错误,例如在json中你的model_joblist等。
share more code and logcat please. 请分享更多代码和logcat。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.