process'command'/usr/lib/jvm/java-11-openjdk-amd64/bin/java''以非零出口值1结束

btqmn9zl  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(302)

我试图使用org.json库在线使用一个json文件,但是当我用gradle运行它时,它会给我错误:

at org.json.JSONTokener.syntaxError(JSONTokener.java:507)
    at org.json.JSONObject.<init>(JSONObject.java:222)
    at org.json.JSONObject.<init>(JSONObject.java:406)
    at me.h0rb.bot.Main.readJsonFromUrl(Main.java:42)
    at me.h0rb.bot.Main.main(Main.java:50)

FAILURE: Build failed with an exception.

* What went wrong:

Execution failed for task ':Main.main()'.
> Process 'command '/usr/lib/jvm/java-11-openjdk-amd64/bin/java'' finished with non-zero exit value 1

我想不出问题所在,代码如下:

package me.h0rb.bot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

public class Main {

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    public static void main(String[] args) throws IOException, JSONException {
        JSONObject json = readJsonFromUrl("https://2b2t.io/api/queue");
        System.out.println(json.toString());
        System.out.println(json.get("id"));
    }
}
brvekthn

brvekthn1#

如果我打开url,我会看到一个json数组(数组以 [ )而不是对象(以 { ).

[[1610033745,1813],[1610033584,1813],[1610033424,1902],[1610033263,1893],[1610033103,1899],[1610032942,1893],[1610032781,1897],[1610032621,1895],[1610032460,1883],[1610032300,1883],[1610032139,1876],[1610031968,1863],[1610031808,1860],[1610031647,1844],[1610031486,1841],[1610031326,1834],[1610031165,1816],[1610031005,1814],[1610030844,1808],[1610030683,1798],[1610030523,1802],[1610030362,1787],[1610030202,1789],[1610030041,1781],[1610029881,1774],[1610029720,1759],[1610029559,1742],[1610029399,1745],[1610029238,1754],[1610029078,1748],[1610028917,1753],[1610028747,1744],[1610028586,1740],[1610028425,1728],[1610028265,1722],[1610028104,1713],[1610027944,1706],[1610027783,1697],[1610027623,1691],[1610027462,1704],[1610027302,1694],[1610027141,1695],[1610026980,1678],[1610026820,1660],[1610026659,1640],[1610026499,1634],[1610026338,1608],[1610026178,1587],[1610026017,1570],[1610025857,1559],[1610025696,1548],[1610025536,1509],[1610025375,1467],[1610025215,1427],[1610025054,1406],[1610024894,1382],[1610024733,1334],[1610024573,1287],[1610024412,1257],[1610024252,1242],[1610024092,1236],[1610023931,1278],[1610023771,1251],[1610023610,1244],[1610023450,1242],[1610023280,1245],[1610023119,1240],[1610022959,1240],[1610022798,1220],[1610022638,1286],[1610022477,1286],[1610022317,1273],[1610022157,1246],[1610021996,1225],[1610021836,1209],[1610021665,1200],[1610021505,1202],[1610021345,1197],[1610021184,1245],[1610021024,1224],[1610020863,1214],[1610020703,1209],[1610020543,1201],[1610020382,1209],[1610020222,1209],[1610020061,1207],[1610019891,1214],[1610019731,1217],[1610019570,1222],[1610019410,1210],[1610019249,1214],[1610019089,1225],[1610018929,1226],[1610018768,1219],[1610018608,1218],[1610018447,1224],[1610018287,1225],[1610018127,1212],[1610017966,1207],[1610017806,1206],[1610017645,1208],[1610017485,1204],[1610017325,1203],[1610017164,1204],[1610017004,1210],[1610016843,1205],[1610016683,1195],[1610016523,1201],[1610016362,1200],[1610016202,1194],[1610016041,1196],[1610015881,1196],[1610015721,1287],[1610015560,1280],[1610015400,1280],[1610015239,1274],[1610015079,1269],[1610014918,1274],[1610014758,1261],[1610014598,1277],[1610014437,1276],[1610014277,1280],[1610014116,1279],[1610013956,1271],[1610013796,1265],[1610013635,1272],[1610013475,1257],[1610013314,1248],[1610013154,1251],[1610012994,1243],[1610012833,1408],[1610012673,1406],[1610012512,1403],[1610012352,1399],[1610012191,1388],[1610012031,1398],[1610011870,1401],[1610011710,1398],[1610011550,1397],[1610011389,1387],[1610011229,1387],[1610011068,1384],[1610010908,1379],[1610010747,1353],[1610010587,1343],[1610010426,1343],[1610010266,1343],[1610010106,1337],[1610009945,1315],[1610009785,1306],[1610009624,1293],[1610009464,1275],[1610009304,1244],[1610009143,1232],[1610008983,1214],[1610008822,1177],[1610008662,1176],[1610008502,1178],[1610008341,1180],[1610008181,1350],[1610008020,1350],[1610007860,1359],[1610007700,1348],[1610007539,1337],[1610007379,1307],[1610007218,1281],[1610007058,1239],[1610006897,1213],[1610006737,1183],[1610006577,1150],[1610006416,1096],[1610006256,1045],[1610006096,1007],[1610005935,975],[1610005775,908],[1610005615,954],[1610005454,937],[1610005294,1000],[1610005124,974],[1610004963,960],[1610004803,948],[1610004643,942],[1610004483,917],[1610004322,900],[1610004162,896],[1610004002,879],[1610003841,849],[1610003681,824],[1610003521,780],[1610003360,725],[1610003200,641],[1610003040,558],[1610002880,1],[1610002667,1103],[1610002506,1105],[1610002346,1100],[1610002185,1098],[1610002025,1096],[1610001865,1090],[1610001704,1086],[1610001544,1094],[1610001384,1083],[1610001223,1078],[1610001063,1069],[1610000903,1067],[1610000742,1067],[1610000582,1065],[1610000422,1065],[1610000261,1073],[1610000101,1066],[1609999941,1063],[1609999780,1069],[1609999620,1074],[1609999460,1072],[1609999299,1079],[1609999139,1068],[1609998979,1071],[1609998818,1072],[1609998658,1066],[1609998498,1066],[1609998337,1059],[1609998177,1037],[1609998016,1041],[1609997856,1006],[1609997696,1000],[1609997535,981],[1609997375,965],[1609997215,957],[1609997054,954],[1609996894,953],[1609996734,953],[1609996573,958],[1609996413,958],[1609996253,963],[1609996093,955],[1609995932,953],[1609995772,956],[1609995612,961],[1609995451,959],[1609995291,967],[1609995131,969],[1609994970,956],[1609994810,959],[1609994649,967],[1609994489,973],[1609994329,968],[1609994168,974],[1609994008,971],[1609993848,960],[1609993687,966],[1609993527,964],[1609993367,967],[1609993206,964],[1609993046,961],[1609992886,968],[1609992725,972],[1609992565,976],[1609992405,971],[1609992244,960],[1609992084,967],[1609991924,972],[1609991763,963],[1609991603,971],[1609991443,981],[1609991282,972],[1609991122,969],[1609990962,971],[1609990801,977],[1609990641,984],[1609990481,982],[1609990320,990],[1609990160,989],[1609990000,982],[1609989839,982],[1609989679,976],[1609989519,982],[1609989358,989],[1609989198,1063],[1609989037,1061],[1609988877,1056],[1609988717,1057],[1609988556,1059],[1609988396,1061],[1609988236,1062],[1609988075,1063],[1609987915,1056],[1609987754,1057],[1609987594,1055],[1609987434,1054],[1609987273,1047],[1609987113,1048],[1609986953,1046],[1609986792,1055],[1609986632,1048],[1609986471,1046],[1609986311,1060],[1609986151,1056],[1609985990,1063],[1609985830,1072],[1609985670,1079],[1609985509,1068],[1609985349,1076],[1609985188,1080],[1609985028,1080],[1609984868,1074],[1609984707,1069],[1609984547,1067],[1609984387,1064],[1609984226,1064],[1609984066,1060],[1609983905,1057],[1609983745,1052],[1609983585,1052],[1609983424,1052],[1609983264,1052],[1609983104,1046],[1609982943,1041],[1609982783,1040],[1609982623,1045],[1609982462,1052],[1609982302,1051],[1609982141,1047],[1609981981,1033],[1609981821,1022],[1609981660,1020],[1609981500,1183],[1609981340,1173],[1609981179,1164],[1609981019,1157],[1609980858,1161],[1609980698,1154],[1609980538,1147],[1609980377,1200],[1609980217,1197],[1609980056,1171],[1609979896,1147],[1609979736,1120],[1609979575,1112],[1609979415,1108],[1609979254,1092],[1609979094,1078],[1609978934,1055],[1609978773,1040],[1609978613,1195],[1609978452,1200],[1609978292,1193],[1609978132,1196],[1609977971,1198],[1609977811,1198],[1609977650,1203],[1609977490,1192],[1609977330,1196],[1609977169,1195],[1609977009,1201],[1609976848,1206],[1609976688,1204],[1609976527,1207],[1609976367,1206],[1609976207,1201],[1609976046,1206],[1609975886,1205],[1609975725,1196],[1609975565,1204],[1609975405,1204],[1609975244,1205],[1609975084,1198],[1609974923,1196],[1609974763,1182],[1609974602,1184],[1609974442,1174],[1609974282,1168],[1609974121,1169],[1609973961,1168],[1609973800,1152],[1609973640,1170],[1609973480,1169],[1609973319,1172],[1609973159,1171],[1609972998,1183],[1609972838,1180],[1609972678,1179],[1609972517,1179],[1609972357,1167],[1609972196,1165],[1609972036,1158],[1609971876,1159],[1609971715,1161],[1609971555,1168],[1609971394,1171],[1609971234,1168],[1609971074,1160],[1609970913,1149],[1609970753,1148],[1609970592,1223],[1609970432,1223],[1609970271,1233],[1609970111,1234],[1609969951,1234],[1609969790,1230],[1609969630,1240],[1609969469,1245],[1609969309,1246],[1609969149,1242],[1609968988,1244],[1609968828,1248],[1609968667,1242],[1609968507,1247],[1609968346,1249],[1609968186,1249],[1609968025,1220],[1609967865,1204],[1609967705,1192],[1609967544,1161],[1609967384,1157],[1609967224,1119],[1609967063,1079],[1609966903,1071],[1609966742,1035],[1609966582,1022],[1609966422,1150],[1609966261,1159],[1609966101,1155],[1609965940,1148],[1609965780,1153],[1609965619,1161],[1609965459,1158],[1609965299,1165],[1609965138,1161],[1609964978,1162],[1609964818,1166],[1609964657,1172],[1609964497,1174],[1609964336,1180],[1609964176,1177],[1609964016,1178],[1609963855,1183],[1609963695,1165],[1609963534,1159],[1609963374,1176],[1609963213,1175],[1609963053,1185],[1609962893,1179],[1609962732,1179],[1609962562,1182],[1609962401,1183],[1609962241,1199],[1609962071,1190],[1609961910,1195],[1609961750,1188],[1609961589,1192],[1609961429,1189],[1609961269,1192],[1609961108,1199],[1609960948,1205],[1609960787,1203],[1609960627,1200],[1609960456,1188],[1609960296,1173],[1609960136,1180],[1609959975,1179],[1609959805,1175],[1609959644,1164],[1609959484,1178],[1609959324,1169],[1609959163,1164],[1609959003,1155],[1609958842,1149],[1609958682,1154],[1609958522,1146],[1609958361,1147],[1609958201,1124],[1609958040,1106],[1609957870,1084],[1609957710,1058],[1609957539,1186],[1609957379,1164],[1609957218,1123],[1609957058,1078],[1609956898,1039],[1609956737,1004],[1609956577,950],[1609956407,916],[1609956246,877],[1609956076,1051],[1609955915,1034],[1609955755,1012],[1609955595,983],[1609955434,949],[1609955274,919],[1609955114,872],[1609954953,830],[1609954793,750],[1609954623,686],[1609954463,591],[1609954302,528],[1609954142,434],[1609953972,359],[1609953812,293],[1609953652,216],[1609953492,130],[1609953331,36],[1609953171,1387],[1609953011,1351],[1609952850,1332],[1609952690,1253],[1609952529,1396],[1609952369,1346],[1609952208,1795],[1609952048,1766],[1609951887,1722],[1609951726,1692],[1609951566,1689],[1609951405,1807],[1609951245,1953],[1609951084,1938],[1609950924,1899],[1609950763,1840],[1609950602,2012],[1609950441,1938],[1609950281,1886],[1609950120,1853],[1609949960,1858],[1609949799,1890],[1609949638,1885],[1609949478,1888],[1609949317,1889],[1609949157,1884],[1609948996,1879],[1609948835,1874],[1609948675,1849],[1609948514,1958],[1609948353,1942],[1609948193,1914],[1609948032,1880],[1609947872,1870],[1609947711,1856],[1609947550,1848]]

您实际上有一个数组,而不是一个json对象。所以我会用 JSONArray 而不是 JSONObject 在你的代码里。

相关问题