简体   繁体   English

Java:将JSON对象附加到JSON对象

[英]Java: Append JSON Object to a JSON Object

I want to store chat history in a JSON Object, one object for each conversation, within this object i want to have an Array with all the messages. 我想将聊天记录存储在一个JSON对象中,每个对话一个对象,在这个对象中,我想拥有一个包含所有消息的数组。

Like this: 像这样:

{"Channel_123":[
           {"from":"john","to":"bill", "msg":"Hello", "time":"09:57"},
           {"from":"bill","to":"john", "msg":"Hey John", "time":"09:58"}
         ]
}, {"Channel_234":[
           {"from":"bob","to":"judy", "msg":"Hello", "time":"10:37"},
           {"from":"judy","to":"bob", "msg":"Hey!", "time":"10:38"}
         ]
}

My current method looks like this: (the string channel contains the conversation id illustrated above with Channel_234 我当前的方法如下所示:(字符串channel包含上面用Channel_234所示的对话ID

JSONObject obj = new JSONObject();
JSONObject msgObj = new JSONObject();

public void addMessage(String from, String to, String msg, String time, String channel) {

    try {
        msgObj.put("from", from);
        msgObj.put("to", to);
        msgObj.put("msg", msg);
        msgObj.put("time", time);


        obj.put(channel, array);

    } catch (Exception ex) {
        System.out.println(ex.getStackTrace());
        logger.log(Level.SEVERE, "Exception: ", ex);
    }

    System.out.println(obj);
}

But for some reason the method is not appending to the object it is overwriting what was previously there. 但是由于某种原因,该方法未附加到对象,因此将覆盖以前存在的对象。

Any ideas? 有任何想法吗?

EDIT 编辑

I am using the simple json lib: json-simple-1.1.1.jar 我正在使用简单的json库: json-simple-1.1.1.jar

I am not familiar with the jason-simple API but you should create a new instance of JSONObject for each item. 我不熟悉jason-imple API,但您应该为每个项目创建一个新的JSONObject实例。

    private JSONObject createMesssage(String form, String to, String msg, String time) throws Exception {

       JSONObject jsonMessage= new JSONObject();

            jsonMessage.put("from", from);
            jsonMessage.put("to", to);
            jsonMessage.put("msg", msg);
            jsonMessage.put("time", time);

       return jsonMessage;  

    }

  JSONObject obj = new JSONObject();

  private void addMessage(String channel, JSonObject messsage) throw Exception {
               obj.put(channel, message);
   }

  public void saveMessage(String form, String to, String msg, String time, Striing channel ) {

    try {
      addMessage(chanell,createMessage(form,to,msg,time));

    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Exception: ", ex);
    }
  }

A valid JSON data representation won't duplicate keys in the same nesting level. 有效的JSON数据表示形式不会在相同的嵌套级别中重复键。 Keeping in mind that only numbers, strings, null, objects and arrays are valid JSON primitives, you have some structure like: 请记住,只有数字,字符串,空值,对象和数组是有效的JSON原语,您具有以下结构:

[
  {
    ...
    "id_str" : "foo",
    ...
  },
  {
    ...
    "id_str" : "bar",
    ...
  },
  ...
]

Which is an array of JSON objects. 这是JSON对象的数组。 In this case, you'd have to index the 4th element of the array and then index the "id_str" attribute of that object. 在这种情况下,您必须索引数组的第4个元素,然后索引该对象的“ id_str”属性。 Check the documentation of whatever JSON library you're using to decode the data to figure out how to do that. 检查用于解码数据的任何JSON库的文档,以了解如何做到这一点。

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

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