繁体   English   中英

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

[英]Parse.com add JSON Object to JSON Array

我的问题很简单,我想将JSONObject添加到我存储在MongoDB数据库中的JSONArray 我可以轻松添加所有类型的数据,如StringsInts等,但不能添加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");
   }
}

但是我的数据库中没有任何内容出现,并且没有抛出任何错误。 你们有任何关于如何做到这一点的线索吗?

尝试使用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");

希望这会有所帮助〜

将该Json对象转换为String并以字符串格式存储它

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

当你想要使用它时,你可以像这样轻松地将它转换为Json Object

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM