{ "personDatas": [ { "indexCodes": [ "3336106" ], "personDataType": "person" } ], "resourceInfos": [ { "resourceIndexCode": "6c031cf1e80b4ebf96a2d39f52c9d97f", "resourceType": "door", "channelNos": null }, { "resourceIndexCode": "c33ed005a5e14ea395bea03f9990c7b9", "resourceType": "door", "channelNos": null }, { "resourceIndexCode": "62651900742b435fa96080d631de773a", "resourceType": "door", "channelNos": null }, { "resourceIndexCode": "6f42a4386d9d4593b4ff69fd519e4432", "resourceType": "door", "channelNos": null }, { "resourceIndexCode": "0b539fe8595746febf41a6ef8bef5006", "resourceType": "door", "channelNos": null }, { "resourceIndexCode": "1c09ee9bfe0c4761b33f968f131d3f99", "resourceType": "door", "channelNos": null }, { "resourceIndexCode": "231b5f298e3b46229fac44f3b3a4eb0b", "resourceType": "door", "channelNos": null } ], "startTime": "2023-06-02T00:00:00+08:00", "endTime": "2043-06-01T23:59:59+08:00" } 如何用java实现这样的报文,并把它装到名为remap的map里面
时间: 2023-07-03 17:32:26 浏览: 121
可以使用Java中的JSONObject和JSONArray类来构造这个报文,然后将其添加到一个名为remap的Map中。
代码如下:
```
import java.util.*;
import org.json.*;
public class Example {
public static void main(String[] args) throws JSONException {
// 构造personDatas数组
JSONArray personDatas = new JSONArray();
JSONObject personData = new JSONObject();
JSONArray indexCodes = new JSONArray();
indexCodes.put("3336106");
personData.put("indexCodes", indexCodes);
personData.put("personDataType", "person");
personDatas.put(personData);
// 构造resourceInfos数组
JSONArray resourceInfos = new JSONArray();
JSONObject resourceInfo = new JSONObject();
resourceInfo.put("resourceIndexCode", "6c031cf1e80b4ebf96a2d39f52c9d97f");
resourceInfo.put("resourceType", "door");
resourceInfo.put("channelNos", JSONObject.NULL);
resourceInfos.put(resourceInfo);
resourceInfo = new JSONObject();
resourceInfo.put("resourceIndexCode", "c33ed005a5e14ea395bea03f9990c7b9");
resourceInfo.put("resourceType", "door");
resourceInfo.put("channelNos", JSONObject.NULL);
resourceInfos.put(resourceInfo);
resourceInfo = new JSONObject();
resourceInfo.put("resourceIndexCode", "62651900742b435fa96080d631de773a");
resourceInfo.put("resourceType", "door");
resourceInfo.put("channelNos", JSONObject.NULL);
resourceInfos.put(resourceInfo);
resourceInfo = new JSONObject();
resourceInfo.put("resourceIndexCode", "6f42a4386d9d4593b4ff69fd519e4432");
resourceInfo.put("resourceType", "door");
resourceInfo.put("channelNos", JSONObject.NULL);
resourceInfos.put(resourceInfo);
resourceInfo = new JSONObject();
resourceInfo.put("resourceIndexCode", "0b539fe8595746febf41a6ef8bef5006");
resourceInfo.put("resourceType", "door");
resourceInfo.put("channelNos", JSONObject.NULL);
resourceInfos.put(resourceInfo);
resourceInfo = new JSONObject();
resourceInfo.put("resourceIndexCode", "1c09ee9bfe0c4761b33f968f131d3f99");
resourceInfo.put("resourceType", "door");
resourceInfo.put("channelNos", JSONObject.NULL);
resourceInfos.put(resourceInfo);
resourceInfo = new JSONObject();
resourceInfo.put("resourceIndexCode", "231b5f298e3b46229fac44f3b3a4eb0b");
resourceInfo.put("resourceType", "door");
resourceInfo.put("channelNos", JSONObject.NULL);
resourceInfos.put(resourceInfo);
// 构造JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("personDatas", personDatas);
jsonObject.put("resourceInfos", resourceInfos);
jsonObject.put("startTime", "2023-06-02T00:00:00+08:00");
jsonObject.put("endTime", "2043-06-01T23:59:59+08:00");
// 将JSONObject添加到Map中
Map<String, Object> remap = new HashMap<String, Object>();
remap.put("jsonObject", jsonObject);
// 输出Map
System.out.println(remap);
}
}
```
输出结果为:
```
{jsonObject={"personDatas":[{"indexCodes":["3336106"],"personDataType":"person"}],"resourceInfos":[{"resourceIndexCode":"6c031cf1e80b4ebf96a2d39f52c9d97f","channelNos":null,"resourceType":"door"},{"resourceIndexCode":"c33ed005a5e14ea395bea03f9990c7b...```
阅读全文