Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Parse.com将JSON对象添加到JSON数组_Java_Android_Json_Mongodb_Parse Platform - Fatal编程技术网

Java Parse.com将JSON对象添加到JSON数组

Java Parse.com将JSON对象添加到JSON数组,java,android,json,mongodb,parse-platform,Java,Android,Json,Mongodb,Parse Platform,我的问题很简单,我想将JSONObject添加到存储在MongoDB数据库中的JSONArray。我可以轻松添加所有类型的数据,如字符串,整数等,但不能添加JSONObjects。 我在代码中执行此操作: public void done(ParseObject lan, ParseException e) { if(e==null){ JSONObject object = new JSONObject(); try{

我的问题很简单,我想将
JSONObject
添加到存储在MongoDB数据库中的
JSONArray
。我可以轻松添加所有类型的数据,如
字符串
整数
等,但不能添加
JSONObjects
。 我在代码中执行此操作:

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");
   }
}
但是我的数据库中没有显示任何内容,也没有抛出任何错误。
你们知道怎么做吗?

将Json对象转换成字符串并以字符串格式存储

lan.add("Participants",object.toString());
当您想要使用它时,您可以像这样轻松地再次将其转换为Json对象

JSONObject jObj=new JSONObject("Your Json String");

尝试使用
object.toString()
而不是
object

lan.add("Participants", object.toString());
JSON:

{"Participants":["{\"PlayerName\":\"John\",\"ID\":514145}"]}
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");
要解析此JSON,请尝试以下操作:

{"Participants":["{\"PlayerName\":\"John\",\"ID\":514145}"]}
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");

希望这会有所帮助~

好的,谢谢,这是可行的,但是在DB中它看起来是这样的:“{\'PlayerName\':\'John\',\'ID\':514145}”。它不是很干净!尝试将其转换回来,并检查是否得到相同的对象,但如何检索它?在数据库中是这样的:“参与者”:[“{\“PlayerName\”:“John\”,“ID\”:514145}],我在回答中已经提到了如何转换回object是的,我可以看到,但是我的JSONObject在数组中,没有“name”。那么,在我的情况下,你会怎么做呢?好的,谢谢,这是可行的,但是在DB中它看起来是这样的:“{\'PlayerName\':'John\',\'ID\':514145}”。它不是很干净!如果我的回答似乎有用,那么请投赞成票。阅读最后一个问题:为什么我们不能向数据库发送一个JSONObject?以后检索它会容易得多不?你能试试
JSONArray JSONArray=new JSONArray()吗;jsonArray.put(对象);jsonArray.put(15);put(JSONObject.null);lan.put(“参与者”,jsonArray)
而不是直接添加到
ParseObject
?我已经尝试过了,但在我的例子中,我希望能够将多个对象添加到数组中。如果我这样做,它将用一个新的阵列替换阵列…好的。我不知道为什么你在岗位上的方式不起作用。是否有一种方法可以从ParseObject获取
JSONArray JSONArray=getExistingArrayFromParseObject