我想知道如何将 json 数组作为 json 对象中的键传递。
{
"name" :"Sam",
"grades": [{"maths": "A", "result":"pass"}, {"science": "B", "result":"pass"}]
}
我无法将这两个值传递给 jSONObject 中的“grades”。我循环播放了。但是,它只是覆盖了这些值。
最佳答案
看起来你正在做类似的事情:
obj.put("grades", mathGrade);
obj.put("grades", scienceGrade);
其中 scienceGrade 只是覆盖 mathGrade。
您应该做的是使用中间数组对象:
JSONArray grades = new JSONArray();
grades.put(mathGrade);
grades.put(scienceGrade);
obj.put("grades", grades);
关于java - 以键值对的形式传递 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13873475/