我的问题很简单,我想添加一个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