我想处理以我不需要的对象开始的JSON数据。
这里有URL:
我一直在努力调整下一个代码,但我不知道如何避开第一个对象(摘要)并采取第二个(资源)。
如果我想逐个采取来自"资源"的每个对象内的所有数据。 (例如,显示" nombre-calle"," tipo-via" ...)。
package leerjson;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class LeerJSON {
public static void main(String[] args) throws ParseException {
JSONParser parser = new JSONParser();
try {
URL oracle = new URL("http://datos.santander.es/api/rest/datasets/callejero_calles.json?items=819"); // URL to Parse
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
in.readLine();
while ((inputLine = in.readLine()) != null) {
JSONArray a = (JSONArray) parser.parse(inputLine);
// Loop through each item
for (Object o : a) {
JSONObject datos = (JSONObject) o;
System.out.println(datos);
}
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
更新: 一旦看到Enra64的回答,我就不知道如何使用getJSONArray和getJSONObject,因为它不是一种方法。我已将json-simple-1.1.1.jar包含在我的项目中,但它不起作用。先感谢您!这是我的新代码:
URL oracle = new URL("http://datos.santander.es/api/rest/datasets/callejero_calles.json?items=819"); // URL to Parse
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine = in.readLine();
JSONObject a = (JSONObject) parser.parse(inputLine);
JSONArray resources = a.getJSONArray("resources");
for (int i = 0; i < resources.length(); i++) {
resources.getJSONObject(i);
}
答案 0 :(得分:2)
选择资源对象,如下所示:
for (int i = 0; i < resources.length(); i++) {
resources.getJSONObject(i);
}
然后循环播放:
@RELATION data_weka
@ATTRIBUTE class {1,0}
@ATTRIBUTE 1 NUMERIC
.
.
.
@ATTRIBUTE 30 NUMERIC
@DATA
1,17.99,10.38,122.8,1001,0.1184,0.2776,0.3001,0.1471,0.2419,0.07871,1.095,0.9053,8.589,153.4,0.006399,0.04904,0.05373,0.01587,0.03003,0.006193,25.38,17.33,184.6,2019,0.1622,0.6656,0.7119,0.2654,0.4601,0.1189
.
.
.