我正在尝试使用这种精确的 JSON 语法,但无法实现:
{
"sun":"yellow",
"vegetables":[{
"apple":"red",
"banana":"yellow",
"melon":"orange"
}]
}
我能得到的最接近的是:
{
"sun":"yellow",
"vegetables":["{
"apple":"red",
"banana":"yellow",
"melon":"orange"
"]}
}
这就是我正在做的:
JSONObject json = new JSONObject();
json.put("sun","yellow");
ArrayList<HashMap<String,Object>> test = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> params = new HashMap<String, Object>();
params.put("apple","red");
params.put("banana","yellow");
params.put("melon","orange");
test.add(params);
json.put("fruits",test);
我想不通,我是不是漏掉了什么?
最佳答案
应该是这样的——
JSONObject json = new JSONObject();
json.put("sun","yellow");
JSONArray veg = new JSONArray();
JSONObject vegData = new JSONObject();
vegData.put("apple","red");
vegData.put("banana","yellow");
vegData.put("melon","orange");
veg.put(vegData);
json.put("vegetables",veg);
我正在使用 Jettison
。详情可以找here .
关于java - 使用 JAVA 的 JSONObject 中的参数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042437/