我的問題很簡單,我想添加一個JSONObject
到存儲在MongoDB數據庫中的JSONArray
。我可以輕鬆地添加所有類型的數據,如Strings
,Ints
等,但不是JSONObjects
。 我這樣做在我的代碼:Parse.com將JSON對象添加到JSON數組
public void done(ParseObject lan, ParseException e) {
if(e==null){
JSONObject object = new JSONObject();
try{
object.put("PlayerName","John");
object.put("ID",514145);
lan.add("Participants",object); //nothing gets inserted
lan.add("Participants",15); //works fine
lan.add("Participants",JSONObject.null); //works fine too
}catch (JSONException error){
}
lan.increment("Number");
lan.saveInBackground();
}else{
Log.i("Parse Error","error");
}
}
但沒有出現在我的數據庫,並沒有拋出錯誤。 你們有什麼線索怎麼做?
你可以嘗試'JSONArray jsonArray =新JSONArray(); jsonArray.put(對象); jsonArray.put(15); jsonArray.put(JSONObject.null); lan.put(「Participants」,jsonArray)'而不是直接添加到'ParseObject'? – Veeram
我試過了,但在我的情況下,我想能夠添加多個對象到我的數組。如果我這樣做,它將用一個新陣列取代陣列... –
好的。我不知道爲什麼你在帖子中的方式不起作用。有沒有辦法可以得到'JSONArray jsonArray = getExistingArrayFromParseObject;'然後添加新條目。 – Veeram