我试图构建以下形式的json数组
[
"1-35e03ac0b6ba442bb0d5fab3faef32fe",
{
"request": [
"UpdateComplianceDetail",
{
"remarks": "Remarks",
"next_due_date": "27-Mar-2016",
"completion_date": "04-Apr-2016",
"validity_date": null,
"documents": null,
"compliance_history_id": 23
}
],
"session_token": "1-35e03ac0b6ba442bb0d5fab3faef32fe"
}
]
以下是我用来创建上述结构的json的代码,
private JSONArray getJsonArray(String encodedString) {
JSONArray docArray = new JSONArray();
JSONObject innerJson = new JSONObject();
JSONArray innerJsonArray = new JSONArray();
JSONObject inJobj = new JSONObject();
JSONArray outerJsonArray = new JSONArray();
SharedPreferences userDetails = getSharedPreferences("userdetails", Context.MODE_PRIVATE);
String session = userDetails.getString("session", "");
try {
innerJson.put("compliance_history_id", 23);
docArray.put("null");
innerJson.put("documents", docArray);
innerJson.put("completion_date", "04-Apr-2016");
innerJson.put("validity_date", mValidityDate.getText().toString());
innerJson.put("next_due_date", mNextDueDate.getText().toString());
innerJson.put("remarks", mRemarks.getText().toString());
innerJsonArray.put("UpdateComplianceDetail");
innerJsonArray.put(innerJson);
inJobj.put("session_token", session);
inJobj.put("request", innerJsonArray);
outerJsonArray.put(session);
outerJsonArray.put(inJobj);
} catch (JSONException e) {
e.printStackTrace();
}
return outerJsonArray;
}
以下是我的代码正在创建的json,
[
"1-35e03ac0b6ba442bb0d5fab3faef32fe",
{
"request": [
"UpdateComplianceDetail",
{
"remarks": "Remarks",
"next_due_date": "27-Mar-2016",
"completion_date": "04-Apr-2016",
"validity_date": "null",
"documents": [
"null"
],
"compliance_history_id": 23
}
],
"session_token": "1-35e03ac0b6ba442bb0d5fab3faef32fe"
}
]
我怎样才能解决这个问题?我在json数组中放了“null”,但它被识别为字符串而不是null。 validity_date也必须为null,但它是一个字符串“null”
答案 0 :(得分:1)
尝试使用JSONObject.NULL
参考: http://developer.android.com/reference/org/json/JSONObject.html#NULL How do you set a value to null with org.json.JSONObject in java?
答案 1 :(得分:1)
docArray.length > 0 ? docArray : null
评估docArray
的长度。如果它的长度大于0(也就是说,它包含项目),它将把数组作为documents
的内容。否则它会放null
。
validity_date
也是如此。 isEmpty()
检查null
或""
,如果是这种情况,将使用null
代替mValidityDate.getText()
的实际值。
private JSONArray getJsonArray(String encodedString) {
JSONArray docArray = new JSONArray();
JSONObject innerJson = new JSONObject();
JSONArray innerJsonArray = new JSONArray();
JSONObject inJobj = new JSONObject();
JSONArray outerJsonArray = new JSONArray();
SharedPreferences userDetails = getSharedPreferences("userdetails", Context.MODE_PRIVATE);
String session = userDetails.getString("session", "");
try {
innerJson.put("compliance_history_id", 23);
innerJson.put("documents", docArray.length > 0 ? docArray : JSONObject.NULL);
innerJson.put("completion_date", "04-Apr-2016");
String validityDate = mValidityDate.getText().toString();
innerJson.put("validity_date", validityDate.isEmpty() ? JSONObject.NULL : validityDate.toString());
innerJson.put("next_due_date", mNextDueDate.getText().toString());
innerJson.put("remarks", mRemarks.getText().toString());
innerJsonArray.put("UpdateComplianceDetail");
innerJsonArray.put(innerJson);
inJobj.put("session_token", session);
inJobj.put("request", innerJsonArray);
outerJsonArray.put(session);
outerJsonArray.put(inJobj);
} catch (JSONException e) {
e.printStackTrace();
}
return outerJsonArray;
}