简体   繁体   English

从以不必要数据开头的URL中以Java读取JSON

[英]Read JSON in Java from URL that starts with unnecessary data

I would like to process JSON data started with an object that I don't need. 我想处理以不需要的对象开头的JSON数据。

Here you have the URL: 这里有URL:

I have been trying to adapt next code, but I don't know how to avoid the first object (summary) and take the second one (resources). 我一直在尝试改编下一个代码,但是我不知道如何避免第一个对象(摘要)而采用第二个对象(资源)。

If I want to take one by one all the data inside from each object of "resources" (for example, showing "nombre-calle", "tipo-via"...). 如果我要从“资源”的每个对象中一一拿走所有内部数据(例如,显示“ 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();
        }  
    }  

UPDATED: Once seen Enra64's answer, I don't know how to use getJSONArray and getJSONObject because it is not a method. 更新:一旦看到Enra64的答案,我不知道如何使用getJSONArray和getJSONObject,因为它不是方法。 I have included json-simple-1.1.1.jar to my project, but it doesn't work. 我已经将json-simple-1.1.1.jar包含到我的项目中,但是它不起作用。 Thank you in advance! 先感谢您! This is my new code: 这是我的新代码:

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);
   }

Select the resources object as follows: 选择资源对象,如下所示:

JSONObject a = (JSONObject) parser.parse(inputLine);
JSONArray resources = a.getJSONArray("resources");

And then loop through it: 然后遍历它:

for (int i = 0; i < resources.length(); i++) {
  resources.getJSONObject(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM