生成如下报文用java实现{ "personDatas": [ { "indexCodes": [ "c35f4a4e-8baa-4e88-b6e3-c4060135e7e6" ], "personDataType": "person" } ], "resourceInfos": [ { "resourceIndexCode": "0a869b06f4ff4da29949acf006405c39", "resourceType": "acsDevice", "channelNos": [ 1 ] } ], "startTime": "2018-09-03T17:30:08.000+08:00", "endTime": "2018-09-06T17:30:08.000+08:00" }
时间: 2024-01-10 14:03:36 浏览: 184
jdk-8u221-windows-i586.exe 安装包
可以使用以下Java代码生成该报文:
```
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
// 构造personDatas
List<String> indexCodes = new ArrayList<>();
indexCodes.add("c35f4a4e-8baa-4e88-b6e3-c4060135e7e6");
PersonData personData = new PersonData(indexCodes, "person");
List<PersonData> personDatas = new ArrayList<>();
personDatas.add(personData);
// 构造resourceInfos
List<Integer> channelNos = new ArrayList<>();
channelNos.add(1);
ResourceInfo resourceInfo = new ResourceInfo("0a869b06f4ff4da29949acf006405c39", "acsDevice", channelNos);
List<ResourceInfo> resourceInfos = new ArrayList<>();
resourceInfos.add(resourceInfo);
// 构造整个报文
Report report = new Report(personDatas, resourceInfos, "2018-09-03T17:30:08.000+08:00", "2018-09-06T17:30:08.000+08:00");
// 转换为JSON格式
Gson gson = new Gson();
String json = gson.toJson(report);
System.out.println(json);
}
}
class Report {
private List<PersonData> personDatas;
private List<ResourceInfo> resourceInfos;
private String startTime;
private String endTime;
public Report(List<PersonData> personDatas, List<ResourceInfo> resourceInfos, String startTime, String endTime) {
this.personDatas = personDatas;
this.resourceInfos = resourceInfos;
this.startTime = startTime;
this.endTime = endTime;
}
}
class PersonData {
private List<String> indexCodes;
private String personDataType;
public PersonData(List<String> indexCodes, String personDataType) {
this.indexCodes = indexCodes;
this.personDataType = personDataType;
}
}
class ResourceInfo {
private String resourceIndexCode;
private String resourceType;
private List<Integer> channelNos;
public ResourceInfo(String resourceIndexCode, String resourceType, List<Integer> channelNos) {
this.resourceIndexCode = resourceIndexCode;
this.resourceType = resourceType;
this.channelNos = channelNos;
}
}
```
代码中使用了Gson库来将报文对象转换为JSON格式的字符串。
阅读全文