[英]Unable to Convert String to Either JSONArray or JSONObject
I was not able to convert String to JSONArray or JSONObject. 我无法将String转换为JSONArray或JSONObject。 Here is the code below:
这是下面的代码:
JSONArray entries = WebRequest.execute(request);
if(entries!=null){
try{
String temp = entries.getJSONObject(0).getString(WebRequest.CONTENT);
String s = temp.toString();
JSONArray cont = new JSONArray(s);
Toast.makeText(getBaseContext(), cont.toString(), Toast.LENGTH_LONG).show();
}catch(Exception e){
}
}
Here is the String result : 这是String结果:
"[{\\ID_PROJECT\\":528,\\"NM_PROJECT\\":\\"TestProject\\",,\\"NM_TASK\\":\\"TestTask\\"}]" “[{\\ ID_PROJECT \\”:528,\\ “NM_PROJECT \\”:\\ “TestProject \\” ,, \\ “NM_TASK \\”:\\ “TestTask \\”}]”
I was not able to get the toast when using this code. 使用此代码时,我无法吐司。
This is json parsing of your json string 这是您的json字符串的json解析
String OutputData = "";
JSONObject jsonResponse;
try {
/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse = new JSONObject("{\"data\":[{\"ID_PROJECT\":528,\"NM_PROJECT\":\"TestProject\",\"NM_TASK\":\"TestTask\"}]}");
/***** Returns the value mapped by name if it exists and is a JSONArray. ***/
/******* Returns null otherwise. *******/
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");
/*********** Process each JSON Node ************/
int lengthJsonArr = jsonMainNode.length();
for(int i=0; i < lengthJsonArr; i++) {
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
/******* Fetch node values **********/
int project_id = Integer.parseInt(jsonChildNode.optString("ID_PROJECT").toString());
String project_name = jsonChildNode.optString("NM_PROJECT").toString();
String task_name = jsonChildNode.optString("NM_TASK").toString();
OutputData += "Node : \n\n "+ project_id +" | "
+ project_name +" | "
+ task_name +" \n\n ";
}
/************ Show Output on screen/activity **********/
output.setText( OutputData );
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.