Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Problema con JSONArray a JSON validato

    ciao!

    sto facendo qualche test con le librerie apache.
    volevo interrogare un servizio rest, e fare il parsin del json.
    il servizio è questo qui https://httpbin.org/get, che restituisce questo:
    codice:
    {
      "args": {}, 
      "headers": {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
        "Accept-Encoding": "gzip, deflate, br", 
        "Accept-Language": "en-US,en;q=0.5", 
        "Cookie": "_ga=GA1.2.593513573.1485521352; _gat=1", 
        "Dnt": "1", 
        "Host": "httpbin.org", 
        "Referer": "https://httpbin.org/", 
        "Upgrade-Insecure-Requests": "1", 
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"
      }, 
      "origin": "IP_ADDRESS", 
      "url": "https://httpbin.org/get"
    }
    l'ho anche validato su jsonlint.

    con questo codice:
    codice:
    public class Main {
    
        public static void main(String[] args) {
            try {
                String strUrl = "https://httpbin.org/get";
                HttpClient client = HttpClientBuilder.create().build();
                HttpGet request = new HttpGet(strUrl);
                HttpResponse response = client.execute(request);
                JSONArray array = new JSONArray(new JSONTokener(response.getEntity().getContent()));
                System.out.println(array.toString());
    //            for (int i = 0; i < array.length(); i++) {
    //                JSONObject object = array.getJSONObject(i);
    //                System.out.println(object.getString("url"));
    //            }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    
    }
    ottengo questo errore:
    codice:
    Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
        at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
        at org.json.JSONArray.<init>(JSONArray.java:108)
        at com.mp.testmaven.Main.main(Main.java:23)
    qualche idea??

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da fermat Visualizza il messaggio
    Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
    Il punto è che quella response non è un "array" JSON .... è un "object".
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Quote Originariamente inviata da andbin Visualizza il messaggio
    Il punto è che quella response non è un "array" JSON .... è un "object".
    ciao andbin!

    ok, immaginavo.
    però se faccio così:
    codice:
    public class Main {
    
        public static void main(String[] args) {
            try {
                String strUrl = "https://httpbin.org/get";
                HttpClient client = HttpClientBuilder.create().build();
                HttpGet request = new HttpGet(strUrl);
                HttpResponse response = client.execute(request);
                JSONObject obj = new JSONObject(response.getEntity().getContent().toString());
                System.out.println(obj.toString());           
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    
    }
    ottengo:
    codice:
    Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
        at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
        at org.json.JSONObject.<init>(JSONObject.java:195)
        at org.json.JSONObject.<init>(JSONObject.java:319)
        at com.mp.testmaven.Main.main(Main.java:21)
    -----------------------------------------------------------

  4. #4
    ok, così funziona:
    codice:
    public class Main {
    
        public static void main(String[] args) {
            try {
                String strUrl = "https://httpbin.org/get";
                HttpClient client = HttpClientBuilder.create().build();
                HttpGet request = new HttpGet(strUrl);
                HttpResponse response = client.execute(request);
                String json = IOUtils.toString(response.getEntity().getContent(), "UTf-8");
                JSONObject obj = new JSONObject(json);
                System.out.println(obj.get("url"));
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    
    }
    garzie!

  5. #5
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da fermat Visualizza il messaggio
    codice:
    String json = IOUtils.toString(response.getEntity().getContent(), "UTf-8");
    Sì, il charset va ovviamente considerato.

    Nota che la HttpCore ha la EntityUtils che offre dei toString:
    static String toString(HttpEntity entity)
    static String toString(HttpEntity entity, Charset defaultCharset)
    static String toString(HttpEntity entity, String defaultCharset)

    Questi sono "furbi", esaminano prima l'header in response per il charset.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.