Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道如何将 json 数组作为 json 对象中的键传递。
{ "name" :"Sam", "grades": [{"maths": "A", "result":"pass"}, {"science": "B", "result":"pass"}] }
我无法将这两个值都传递给 jSONObject 中的“等级”。我把它循环了。但是,它只是覆盖了这些值。
看起来你正在做类似的事情:
obj.put("grades", mathGrade); obj.put("grades", scienceGrade);
scienceGrade 只是覆盖了 mathGrade。
您应该做的是使用中间数组对象:
JSONArray grades = new JSONArray(); grades.put(mathGrade); grades.put(scienceGrade); obj.put("grades", grades);