Java 3级Json如何在内部进行计数
我想使用一个返回json的api,如下所示:Java 3级Json如何在内部进行计数,java,json,Java,Json,我想使用一个返回json的api,如下所示: { "startDate": "2019-06-23T16:07:21.205Z", "endDate": "2019-07-24T16:07:21.205Z", "status": "Complete", "usages": [ { "name": "PureCloud Edge Virtual Usage", "resources": [ { "name": "E
{
"startDate": "2019-06-23T16:07:21.205Z",
"endDate": "2019-07-24T16:07:21.205Z",
"status": "Complete",
"usages": [
{
"name": "PureCloud Edge Virtual Usage",
"resources": [
{
"name": "Edge01-VM-GNS-DemoSite01 (1f279086-a6be-4a21-ab7a-2bb1ae703fa0)",
"date": "2019-07-24T09:00:28.034Z"
},
{
"name": "329ad5ae-e3a3-4371-9684-13dcb6542e11",
"date": "2019-07-24T09:00:28.034Z"
},
{
"name": "e5796741-bd63-4b8e-9837-4afb95bb0c09",
"date": "2019-07-24T09:00:28.034Z"
}
]
},
{
"name": "PureCloud for SmartVideo Add-On Concurrent",
"resources": [
{
"name": "jpizarro@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "jaguilera@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "dcortes@gns.com.co",
"date": "2019-07-15T15:06:09.203Z"
}
]
},
{
"name": "PureCloud 3 Concurrent User Usage",
"resources": [
{
"name": "jpizarro@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "jaguilera@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "dcortes@gns.com.co",
"date": "2019-07-15T15:06:09.203Z"
}
]
},
{
"name": "PureCloud Skype for Business WebSDK",
"resources": [
{
"name": "jpizarro@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "jaguilera@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "dcortes@gns.com.co",
"date": "2019-07-15T15:06:09.203Z"
}
]
}
],
"selfUri": "/api/v2/billing/reports/billableusage"
}
我必须计算最后一级中的姓名数量。在网上,我发现了一个库,我只是尝试改编这个例子,但我有一些疑问:一个是它以字符串开头,而不是我必须测试的json文件的路径。我已经手动将其放入其中,buy Eclipse添加了很多:/
当我尝试运行此命令时:
String jsonString = "{\r\n" +
" \"startDate\": \"2019-06-23T16:07:21.205Z\",\r\n" +
" \"endDate\": \"2019-07-24T16:07:21.205Z\",\r\n" +
" \"status\": \"Complete\",\r\n" +
" \"usages\": [\r\n" +
" {\r\n" +
" \"name\": \"PureCloud Edge Virtual Usage\",\r\n" +
" \"resources\": [\r\n" +
" {\r\n" +
" \"name\": \"Edge01-VM-GNS-DemoSite01 (1f279086-a6be-4a21-ab7a-2bb1ae703fa0)\",\r\n" +
" \"date\": \"2019-07-24T09:00:28.034Z\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\": \"329ad5ae-e3a3-4371-9684-13dcb6542e11\",\r\n" +
" \"date\": \"2019-07-24T09:00:28.034Z\"\r\n" +
" }, \r\n" +
" {\r\n" +
" \"name\": \"e5796741-bd63-4b8e-9837-4afb95bb0c09\",\r\n" +
" \"date\": \"2019-07-24T09:00:28.034Z\"\r\n" +
" }\r\n" +
" ]\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\": \"PureCloud for SmartVideo Add-On Concurrent\",\r\n" +
" \"resources\": [\r\n" +
" {\r\n" +
" \"name\": \"jpizarro@gns.com.co\",\r\n" +
" \"date\": \"2019-06-25T04:54:17.662Z\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\": \"jaguilera@gns.com.co\",\r\n" +
" \"date\": \"2019-06-25T04:54:17.662Z\"\r\n" +
" }, \r\n" +
" {\r\n" +
" \"name\": \"dcortes@gns.com.co\",\r\n" +
" \"date\": \"2019-07-15T15:06:09.203Z\"\r\n" +
" }\r\n" +
" ]\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\": \"PureCloud 3 Concurrent User Usage\",\r\n" +
" \"resources\": [\r\n" +
" {\r\n" +
" \"name\": \"jpizarro@gns.com.co\",\r\n" +
" \"date\": \"2019-06-25T04:54:17.662Z\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\": \"jaguilera@gns.com.co\",\r\n" +
" \"date\": \"2019-06-25T04:54:17.662Z\"\r\n" +
" }, \r\n" +
" {\r\n" +
" \"name\": \"dcortes@gns.com.co\",\r\n" +
" \"date\": \"2019-07-15T15:06:09.203Z\"\r\n" +
" }\r\n" +
" ]\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\": \"PureCloud Skype for Business WebSDK\",\r\n" +
" \"resources\": [\r\n" +
" {\r\n" +
" \"name\": \"jpizarro@gns.com.co\",\r\n" +
" \"date\": \"2019-06-25T04:54:17.662Z\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\": \"jaguilera@gns.com.co\",\r\n" +
" \"date\": \"2019-06-25T04:54:17.662Z\"\r\n" +
" }, \r\n" +
" {\r\n" +
" \"name\": \"dcortes@gns.com.co\",\r\n" +
" \"date\": \"2019-07-15T15:06:09.203Z\"\r\n" +
" }\r\n" +
" ]\r\n" +
" }\r\n" +
" ],\r\n" +
" \"selfUri\": \"/api/v2/billing/reports/billableusage\"\r\n" +
"}\r\n" +
"";
int cantidadDeLicencias = 0;
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray usages = jsonObject.getJSONArray("usages");
System.out.println("usages " + usages);
for (int i = 0; i < usages.length(); i++) {
JSONArray resources = jsonObject.getJSONArray("resources");
for (int j = 0; j < resources.length(); j++) {
JSONArray names = jsonObject.getJSONArray("name");
cantidadDeLicencias = cantidadDeLicencias + names.length();
//String name = jsonObject.getJSONObject(j).getString("name");
}
}
System.out.println("Cantidad de licencias" + cantidadDeLicencias);
String jsonString=“{\r\n”+
“\'startDate\”:\“2019-06-23T16:07:21.205Z\”,\r\n“+
“\'endDate\”:\“2019-07-24T16:07:21.205Z\”,\r\n“+
“\”状态\“:\”完成\“,\r\n”+
“\“用法\”:[\r\n”+
“{\r\n”+
“\”名称\“:\”PureCloud边缘虚拟使用\“,\r\n”+
“\”资源\“:[\r\n”+
“{\r\n”+
“\”名称\“:\”Edge01-VM-GNS-DemoSite01(1f279086-a6be-4a21-ab7a-2bb1ae703fa0)\”,\r\n“+
“\”日期\“:\”2019-07-24T09:00:28.034Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\“名称\”:\“329ad5ae-e3a3-4371-9684-13dcb6542e11\”,\r\n“+
“\”日期\“:\”2019-07-24T09:00:28.034Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\“名称\”:\“e5796741-bd63-4b8e-9837-4afb95bb0c09\”,\r\n“+
“\”日期\“:\”2019-07-24T09:00:28.034Z\”\r\n“+
“}\r\n”+
“]\r\n”+
“},\r\n”+
“{\r\n”+
“\”名称\“:\”PureCloud for SmartVideo加载项并发\“,\r\n”+
“\”资源\“:[\r\n”+
“{\r\n”+
“\”姓名\“:\”jpizarro@gns.com.co\“,\r\n”+
“\“日期\”:\“2019-06-25T04:54:17.662Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\”姓名\“:\”jaguilera@gns.com.co\“,\r\n”+
“\“日期\”:\“2019-06-25T04:54:17.662Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\”姓名\“:\”dcortes@gns.com.co\“,\r\n”+
“\”日期\“:\”2019-07-15T15:06:09.203Z\”\r\n“+
“}\r\n”+
“]\r\n”+
“},\r\n”+
“{\r\n”+
“\”名称\“:\”PureCloud 3并发用户使用\“,\r\n”+
“\”资源\“:[\r\n”+
“{\r\n”+
“\”姓名\“:\”jpizarro@gns.com.co\“,\r\n”+
“\“日期\”:\“2019-06-25T04:54:17.662Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\”姓名\“:\”jaguilera@gns.com.co\“,\r\n”+
“\“日期\”:\“2019-06-25T04:54:17.662Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\”姓名\“:\”dcortes@gns.com.co\“,\r\n”+
“\”日期\“:\”2019-07-15T15:06:09.203Z\”\r\n“+
“}\r\n”+
“]\r\n”+
“},\r\n”+
“{\r\n”+
“\”名称\“:\”PureCloud Skype for Business WebSDK\”,\r\n“+
“\”资源\“:[\r\n”+
“{\r\n”+
“\”姓名\“:\”jpizarro@gns.com.co\“,\r\n”+
“\“日期\”:\“2019-06-25T04:54:17.662Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\”姓名\“:\”jaguilera@gns.com.co\“,\r\n”+
“\“日期\”:\“2019-06-25T04:54:17.662Z\”\r\n“+
“},\r\n”+
“{\r\n”+
“\”姓名\“:\”dcortes@gns.com.co\“,\r\n”+
“\”日期\“:\”2019-07-15T15:06:09.203Z\”\r\n“+
“}\r\n”+
“]\r\n”+
“}\r\n”+
“],\r\n”+
“\“selfUri\”:\“/api/v2/billing/reports/billableusage\”\r\n“+
“}\r\n”+
"";
int cantidadDeLicencias=0;
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray usages=jsonObject.getJSONArray(“usages”);
System.out.println(“用法”+用法);
for(int i=0;i
出现错误:未找到JSONObject[“资源”]
如果程序可以工作,我必须做的更改会导致什么,api返回一个json,你知道,没有所有这些:/。那么这个图书馆会有用吗
更新 现在它起作用了
int cantidadDeLicencias = 0;
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray usages = jsonObject.getJSONArray("usages");
System.out.println("usages " + usages);
for (int i = 0; i < usages.length(); i++) {
JSONObject innerTemp = usages.getJSONObject(i);
JSONArray resources = innerTemp.getJSONArray("resources");
//JSONArray resources = usages.getJSONArray("resources");
for (int j = 0; j < resources.length(); j++) {
String names = resources.getJSONObject(j).getString("name");
cantidadDeLicencias = cantidadDeLicencias + 1;
//String name = jsonObject.getJSONObject(j).getString("name");
}
}
System.out.println("Cantidad de licencias " + cantidadDeLicencias);
int cantidadDeLicencias=0;
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray usages=jsonObject.getJSONArray(“usages”);
JSONArray usages = jsonObject.getJSONArray("usages");
for (int i = 0; i < usages.length(); i++) {
JSONObject innerTemp = usages.getJSONObject(i);
JSONArray resources = innerTemp.getJSONArray("resources");
// more code here
}