如何将嵌套的Json数组发布到服务器?

时间:2018-08-05 06:26:33

标签: android json post

嗨,我想使用JsonObject将其中包含两个整数的json数组的json数组发布到服务器

"job_experiences": [
        {
            "job_category": [Integer],
            "experience_level": [Integer]
        }
    ]


  JsonArray list = new JsonArray();

            for (int i = 0; i < selectedJobs.size(); i++) {
                list.add(selectedJobs.get(i));
            }
            JsonArray list2 = new JsonArray();

            for (int i = 0; i < selectedJobs.size(); i++) {
                list2.add(selectedJobs.get(i));
            }




            JsonObject postData = new JsonObject();

        postData.add("job_category", list);
        postData.add("experience_level", list2);

1 个答案:

答案 0 :(得分:0)

请使用JSONObject代替JsonObject,用JSONArray代替JsonArray, 请尝试以下方法并检查

JSONArray list = new JSONArray();

for (int i = 0; i < selectedJobs.size(); i++) {
  list.put(selectedJobs.get(i));
}

JSONArray list2 = new JSONArray();

for (int i = 0; i < selectedJobs.size(); i++) {
  list2.put(selectedJobs.get(i));
}


JSONObject postData = new JSONObject();


try {
      postData.put("job_category", list);
      postData.put("experience_level", list2);

      JSONArray job_experiences = new JSONArray();
      job_experiences.put(postData);

      JSONObject jsonObject_job_experiences = new JSONObject();
      jsonObject_job_experiences.put("job_experiences", job_experiences);

      Log.e("jsonObject_job_exp", jsonObject_job_experiences.toString());

    } catch (JSONException e) {
        e.printStackTrace();
    }

,并且在日志中您可以看到最后的JSONObject