我的Json对象:
"feedback_details":{
"id":"80",
"lead_id":"86075",
"account_id":"1543",
"document_id":"582348",
"reason_id":"4",
"description":"COAPPL_profile1.jpg",
"sales_remark":null,
"lat":"",
"long":"",
"status":"2",
"created_at":"2018-02-27 12:29:59",
"updated_at":"2018-02-27 12:29:59"
}
当我在feedback_details中没有任何细节时,它会出现一个空的json数组,如下所示:
"feedback_details":[
]
所以我尝试将其强制转换为空白的json对象:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
for (int j = 0; j < object.getJSONArray("applicantPhoto").length(); j++) {
object.getJSONArray("applicantPhoto").getJSONObject(j);
if (object.getJSONArray("applicantPhoto").getJSONObject(j).optJSONObject("feedback_details") == null) {
new JSONObject(object.getJSONArray("applicantPhoto").getJSONObject(j).getString("feedback_details"));
}
if (object.getJSONArray("applicantSignatire").getJSONObject(j).optJSONObject("feedback_details") == null) {
String newstring = object.getJSONArray("applicantSignatire").getJSONObject(j).getJSONArray("feedback_details").toString();
//here i get the exception for type mismatch where the string is "[]"
new JSONObject(newstring);
}
if (object.getJSONArray("applicantPhoto").getJSONObject(j).optJSONObject("feedback_details") == null) {
new JSONObject(String.valueOf(object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONArray("feedback_details")));
}
}
Utility.printMessage("Json object..." + object);
我知道字符串是&#34; []&#34;因此它显示异常,因为无法转换为JsonObject。 任何人都可以建议如何处理这个,基本上我需要一个空对象而不是那个空白数组。
答案 0 :(得分:0)
String data = "Your Response data";
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
//you have an object
else if (json instance of JSONArray)
//you have an array
另外,请查看以下链接了解详情 Determine whether JSON is a JSONObject or JSONArray
答案 1 :(得分:0)
请检查以下代码
if(!object.getJSONArray("applicantPhoto").getJSONObject(j).isNull("feedback_details")) {
if (object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONObject("feedback_details").toString().length() > 2) {
for (int i = 0; i < object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONObject("feedback_details").length(); i++) {
JSONObject jsonObject1 = object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONObject("feedback_details").getJSONObject(i);
//Get the Values of Feedback_details Key
}
} else {
//Feedback_detail key is empty
}
} else {
//Key is null or Array is empty
}