android - 如何将嵌套的 Json 数组发布到服务器?
问题描述
嗨,我想使用 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);
解决方案
请使用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
推荐阅读
- typescript - 传递数组迭代而不是字符串时出现 spyOn TS 错误
- c++ - 是否可以全局#define 一个宏?(供实时错误检查器查看)
- node.js - 在页面加载时从 Redux 操作中获取状态
- python - Discord.py Emoji 对象错误与 discord 的默认 Unicode 表情符号
- reactjs - 如何解决打字稿和@storybook/addon-knobs 的问题
- google-apps-script - Google App Script 解析 Google Site 上的查询参数
- linux - 同一系统上的普通 RHEL 和 PREEMPT_RT 补丁?可能吗
- bash - Bash 文件:如何打开正确版本的 Matlab
- ecmascript-6 - 没有转译的 ES6 库有没有像“直接”或“干净”这样的术语?
- ios - 是否有任何关于向 WhatsApp for iOS 应用程序发送动画贴纸包的信息?