Json数组的格式不正确

时间:2016-04-04 13:48:53

标签: android json

我试图构建以下形式的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”

2 个答案:

答案 0 :(得分:1)

答案 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;
}