在Java中创建json对象

在Java中创建json对象,java,json,Java,Json,我是Java新手,我正在尝试创建以下JSON格式,下面是我正在尝试的代码,但它不起作用。我正在尝试用下面的代码逻辑生成JSON格式,有人能帮我找到正确的方法吗 当前的JSON: { "phoneCallsMade": [{}], "patientId": "1234558820-HT5", "campaignId": "0", "message"

我是Java新手,我正在尝试创建以下JSON格式,下面是我正在尝试的代码,但它不起作用。我正在尝试用下面的代码逻辑生成JSON格式,有人能帮我找到正确的方法吗

当前的JSON:

 {
    "phoneCallsMade": [{}],
    "patientId": "1234558820-HT5",
    "campaignId": "0",
    "message": [{
        "messageCategory": "ABBB",
        "messageIdentifier": "Z1231ont",
        "messageSequenceNumber": "376",
        "messageSentTime": "2020-11-12 10:06:06.0"
    }]
}, {
    "phoneCallsMade": [{}],
    "patientId": "1234558820-HT5",
    "campaignId": "0",
    "message": [{
        "messageCategory": "TETTTr",
        "messageIdentifier": "bbbbb",
        "messageSequenceNumber": "377",
        "messageSentTime": "2020-11-12 10:06:08.0"
    }]
}, {
    "phoneCallsMade": [{}],
    "patientId": "1234558820-HT5",
    "campaignId": "0",
    "message": [{
        "messageCategory": "TEST",
        "messageIdentifier": "aaaaaaa",
        "messageSequenceNumber": "381",
        "messageSentTime": "2020-11-12 10:07:03.0"
    }]
}, {
    "phoneCallsMade": [{}],
    "patientId": "1234558820-HT5",
    "campaignId": "0",
    "message": [{
        "messageCategory": "ABC",
        "messageIdentifier": "erererer",
        "messageSequenceNumber": "382",
        "messageSentTime": "2020-11-12 10:07:05.0"
    }]
所需的JSON格式:

{
   "report":[
      {
         "patientId":123456789012,
         "campaignId":1234567890,      
         "message":[
             {
                  "messageSequenceNumber":12345678901234567890,
                  "messageIdentifier":"aaaaaaaaaaa",
                  "messageSentTime":"2020-20-08 14:19:05",
                  "messageCategory":"bbbbb"
             }
         ],
         "phoneCallsMade":[
             {
                  "timestamp":"2020-20-08 15:19:05",
                  "outboundPhoneNumber":"1234567890",
                  "duration":"0000-00-00 0:19:05"
             }
         ]
      }
   ]
}
data = new JSONObject();
            
messageDataJSON = new JSONObject();
callDataJSON = new JSONObject();

List<FileVO> messageData = null;            
List<FileVO> callData = null;   

for (FileVO reportVO : reportData) {                
    try {

        data.put("patientId", reportVO.getId());
        data.put("campaignId", reportVO.getEventId());
        
        bw.append(data.toString()+',');

        // Get messages for the patientId/CampaignId
        messageData = FileProcessDao.getMessageData(reportVO.getEventId(), connection,logger);
        
        for (FileVO messageVO : messageData) {
            // Prepare the array for users
            
            JSONArray arraymessage = new JSONArray();
            JSONObject message = new JSONObject();                          
            
            message.put("messageSequenceNumber",messageVO.getMessageSequenceNumber());
            message.put("messageIdentifier",messageVO.getMessageIdentifier());
            message.put("messageSentTime",messageVO.getMessageSentTime());
            message.put("messageCategory",messageVO.getMessageCategory());
            
            arraymessage.put(message);                      
            data.put("message", arraymessage);      
            bw.append(data.toString());
        }
    
        
        // Get call details for the patientId
        callData = FileProcessDao.getCallData(reportVO.getId(), connection,logger);
        
        for (FileVO callVO : callData) {
            JSONArray phoneCallsMade = new JSONArray();     
            JSONObject phones = new JSONObject();
            
            phones.put("timestamp",callVO.getEffectiveDate());
            phones.put("outboundPhoneNumber",callVO.getHcpNumber());
            phones.put("duration",callVO.getCallDuration());
            
            phoneCallsMade.put(phones);     
            data.put("phoneCallsMade", phoneCallsMade);
            bw.append(data.toString());
        }
        
    } catch (Exception e) {
        // TODO Auto-generated catch block
        logger.error("", e);
    }
}
JSONArray report = new JSONArray();
JSONObject finalObject = new JSONObject();

report.put(data);

finalObject.put("report", report);
我试图生成所需的JSON格式的代码:

{
   "report":[
      {
         "patientId":123456789012,
         "campaignId":1234567890,      
         "message":[
             {
                  "messageSequenceNumber":12345678901234567890,
                  "messageIdentifier":"aaaaaaaaaaa",
                  "messageSentTime":"2020-20-08 14:19:05",
                  "messageCategory":"bbbbb"
             }
         ],
         "phoneCallsMade":[
             {
                  "timestamp":"2020-20-08 15:19:05",
                  "outboundPhoneNumber":"1234567890",
                  "duration":"0000-00-00 0:19:05"
             }
         ]
      }
   ]
}
data = new JSONObject();
            
messageDataJSON = new JSONObject();
callDataJSON = new JSONObject();

List<FileVO> messageData = null;            
List<FileVO> callData = null;   

for (FileVO reportVO : reportData) {                
    try {

        data.put("patientId", reportVO.getId());
        data.put("campaignId", reportVO.getEventId());
        
        bw.append(data.toString()+',');

        // Get messages for the patientId/CampaignId
        messageData = FileProcessDao.getMessageData(reportVO.getEventId(), connection,logger);
        
        for (FileVO messageVO : messageData) {
            // Prepare the array for users
            
            JSONArray arraymessage = new JSONArray();
            JSONObject message = new JSONObject();                          
            
            message.put("messageSequenceNumber",messageVO.getMessageSequenceNumber());
            message.put("messageIdentifier",messageVO.getMessageIdentifier());
            message.put("messageSentTime",messageVO.getMessageSentTime());
            message.put("messageCategory",messageVO.getMessageCategory());
            
            arraymessage.put(message);                      
            data.put("message", arraymessage);      
            bw.append(data.toString());
        }
    
        
        // Get call details for the patientId
        callData = FileProcessDao.getCallData(reportVO.getId(), connection,logger);
        
        for (FileVO callVO : callData) {
            JSONArray phoneCallsMade = new JSONArray();     
            JSONObject phones = new JSONObject();
            
            phones.put("timestamp",callVO.getEffectiveDate());
            phones.put("outboundPhoneNumber",callVO.getHcpNumber());
            phones.put("duration",callVO.getCallDuration());
            
            phoneCallsMade.put(phones);     
            data.put("phoneCallsMade", phoneCallsMade);
            bw.append(data.toString());
        }
        
    } catch (Exception e) {
        // TODO Auto-generated catch block
        logger.error("", e);
    }
}
JSONArray report = new JSONArray();
JSONObject finalObject = new JSONObject();

report.put(data);

finalObject.put("report", report);
data=newjsonobject();
messageDataJSON=新的JSONObject();
callDataJSON=新的JSONObject();
List messageData=null;
List callData=null;
对于(FileVO reportVO:reportData){
试一试{
data.put(“patientId”,reportVO.getId());
data.put(“活动ID”,reportVO.getEventId());
append(data.toString()+',');
//获取patientId/CampaignId的消息
messageData=FileProcessDao.getMessageData(reportVO.getEventId(),连接,记录器);
对于(FileVO messageVO:messageData){
//为用户准备阵列
JSONArray arraymessage=新的JSONArray();
JSONObject消息=新的JSONObject();
message.put(“messageSequenceNumber”,messageVO.getMessageSequenceNumber());
message.put(“messageIdentifier”,messageVO.getMessageIdentifier());
message.put(“messageSentTime”,messageVO.getMessageSentTime());
message.put(“messageCategory”,messageVO.getMessageCategory());
arraymessage.put(消息);
data.put(“消息”,数组消息);
append(data.toString());
}
//获取patientId的呼叫详细信息
callData=FileProcessDao.getCallData(reportVO.getId(),连接,记录器);
for(FileVO callVO:callData){
JSONArray phoneCallsMade=新的JSONArray();
JSONObject=newJSONObject();
phones.put(“timestamp”,callVO.getEffectiveDate());
phones.put(“outboundPhoneNumber”,callVO.getHcpNumber());
phones.put(“duration”,callVO.getCallDuration());
电话呼叫made.put(电话);
data.put(“phoneCallsMade”,phoneCallsMade);
append(data.toString());
}
}捕获(例外e){
//TODO自动生成的捕捉块
记录器错误(“,e);
}
}
JSONArray报告=新的JSONArray();
JSONObject finalObject=新的JSONObject();
报告.投入(数据);
finalObject.put(“报告”,报告);

您显示的结果似乎来自第一个
bw.append(data.toString())
(在(FileVO messageVO:messageData){的循环末尾
callData
中是否有任何元素

我认为,对于JsonSerialization,最好使用像or或other这样的库,这样您就可以直接从类对象创建JSON,而无需任何代码


我认为Jackson是最简单的选择。它也是使用最广泛的框架Spring的标准方式。

您呈现的结果似乎来自第一个
bw.append(data.toString())
(在循环的末尾
for(FileVO messageVO:messageData){
callData
中是否有任何元素

我认为,对于JsonSerialization,最好使用像or或other这样的库,这样您就可以直接从类对象创建JSON,而无需任何代码


我认为Jackson是最简单的选择。它也是使用最广泛的框架Spring的标准方式。

对于所需的JSON格式,您可以这样组装:

public static void main(String[] args) {
        Gson gson = new Gson();
        Map<String, List<Object>> outMap = new HashMap<>();
        Map<String, Object> map = new HashMap();
        map.put("patientId", 123456789012L);
        map.put("campaignId", 1234567890);

        Map<String, Object> messageMap = new HashMap();
        messageMap.put("messageSequenceNumber", "12345678901234567890");
        messageMap.put("messageIdentifier","aaaaaaaaaaa");
        messageMap.put("messageSentTime","2020-20-08 14:19:05");
        messageMap.put("messageCategory","bbbbb");
        map.put("message",Collections.singletonList(messageMap));

        Map<String, Object> phoneCallsMadeMap = new HashMap();
        phoneCallsMadeMap.put("timestamp","2020-20-08 15:19:05");
        phoneCallsMadeMap.put("outboundPhoneNumber","1234567890");
        phoneCallsMadeMap.put("duration","0000-00-00 0:19:05");
        map.put("phoneCallsMade", Collections.singletonList(phoneCallsMadeMap));

        outMap.put("report", Collections.singletonList(map));

        System.out.println(gson.toJson(outMap));

    }
publicstaticvoidmain(字符串[]args){
Gson Gson=新的Gson();
Map outMap=newhashmap();
Map Map=newhashmap();
地图放置(“patientId”,123456789012L);
地图放置(“活动ID”,1234567890);
Map messageMap=newhashmap();
messageMap.put(“messageSequenceNumber”,“123456789001234567890”);
put(“messageIdentifier”,“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
messageMap.put(“messageSentTime”,“2020-20-0814:19:05”);
messageMap.put(“messageCategory”、“bbbbb”);
map.put(“message”,Collections.singletonList(messageMap));
Map phoneCallsMadeMap=newhashmap();
电话呼叫数据映射放置(“时间戳”,“2020-20-0815:19:05”);
PhoneCallsMap.put(“outboundPhoneNumber”,“1234567890”);
电话呼叫数据映射put(“持续时间”,“0000-00-00 0:19:05”);
map.put(“phoneCallsMade”,Collections.singletonList(phoneCallsMadeMap));
outMap.put(“报告”,Collections.singletonList(map));
System.out.println(gson.toJson(outMap));
}

对于所需的JSON格式,您可以按如下方式进行组装:

public static void main(String[] args) {
        Gson gson = new Gson();
        Map<String, List<Object>> outMap = new HashMap<>();
        Map<String, Object> map = new HashMap();
        map.put("patientId", 123456789012L);
        map.put("campaignId", 1234567890);

        Map<String, Object> messageMap = new HashMap();
        messageMap.put("messageSequenceNumber", "12345678901234567890");
        messageMap.put("messageIdentifier","aaaaaaaaaaa");
        messageMap.put("messageSentTime","2020-20-08 14:19:05");
        messageMap.put("messageCategory","bbbbb");
        map.put("message",Collections.singletonList(messageMap));

        Map<String, Object> phoneCallsMadeMap = new HashMap();
        phoneCallsMadeMap.put("timestamp","2020-20-08 15:19:05");
        phoneCallsMadeMap.put("outboundPhoneNumber","1234567890");
        phoneCallsMadeMap.put("duration","0000-00-00 0:19:05");
        map.put("phoneCallsMade", Collections.singletonList(phoneCallsMadeMap));

        outMap.put("report", Collections.singletonList(map));

        System.out.println(gson.toJson(outMap));

    }
publicstaticvoidmain(字符串[]args){
Gson Gson=新的Gson();
Map outMap=newhashmap();
Map Map=newhashmap();
地图放置(“patientId”,123456789012L);
地图放置(“活动ID”,1234567890);
Map messageMap=newhashmap();
messageMap.put(“messageSequenceNumber”,“123456789001234567890”);
put(“messageIdentifier”,“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
messageMap.put(“messageSentTime”,“2020-20-0814:19:05”);
messageMap.put(“messageCategory”、“bbbbb”);
map.put(“message”,Collections.singletonList(messageMap));
Map phoneCallsMadeMap=newhashmap();
电话呼叫数据映射放置(“时间戳”,“2020-20-0815:19:05”);
PhoneCallsMap.put(“outboundPhoneNumber”,“1234567890”);
电话呼叫数据映射put(“持续时间”,“0000-00-00 0:19:05”);
map.put(“phoneCallsMade”,Collections.singletonList(phoneCallsMadeMap));
outMap.put(“报告”,Collections.singletonList(map));
System.out.println(gson.toJson(outMap));
}

yes callData中有元素,但不是所有IDYes都有元素