Parse.com将JSON对象添加到JSON数组 [英] Parse.com add JSON Object to JSON Array
本文介绍了Parse.com将JSON对象添加到JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题很简单,我想向存储在MongoDB数据库中的JSONArray
添加JSONObject
.我可以轻松添加所有类型的数据,例如Strings
,Ints
等,但不能添加JSONObjects
.
我在代码中这样做:
My problem is simple, I'd like to add a JSONObject
to a JSONArray
that I store in a MongoDB database. I can easily add all types of data like Strings
, Ints
etc but not JSONObjects
.
I do this in my code :
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");
}
}
但是我的数据库中什么也没有出现,也没有抛出任何错误. 你们对如何做有任何线索吗?
But nothing appears in my db and there's no error thrown. Do you guys have any clue on how to do that ?
推荐答案
尝试使用object.toString()
而不是object
.
lan.add("Participants", object.toString());
JSON:
{"Participants":["{\"PlayerName\":\"John\",\"ID\":514145}"]}
要解析此JSON,请尝试以下操作:
JSONObject jsonObj = new JSONObject(YOUR_JSON_STRING);
// Participants
JSONArray participantsJsonArray = jsonObj.getJSONArray("Participants");
// Participant
JSONObject participanJsonObject = participantsJsonArray.getJSONObject(0);
// PlayerName
String playerName = participanJsonObject.getString("PlayerName");
// ID
String id = participanJsonObject.getInt("ID");
希望这会有所帮助〜
这篇关于Parse.com将JSON对象添加到JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文