2014-10-14 35 views
8

Mój kod wysyła żądanie GET do serwera,Konwersja BufferedReader do JSONObject lub mapą

URL obj = new URL(url); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
    con.setRequestMethod("GET"); 
    con.setRequestProperty("User-Agent", USER_AGENT); 
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 

uzyskać obiekt BufferedReader która drukuje,

{ 
    "status": "ERROR",  
    "errorCode": "MISSING_PARAMS",  
    "errorMessage": null,  
    "requestId": "20141014181739_11625805172",  
    "downstreamModuleErrorCode": null,  
    "object": [   
    "activity_code",   
    "activity_name", 
    "points", 
    "frequency", 
    "strategy", 
    "vsa_app_access_token"  
    ] 
} 

Chcę uzyskać JSONObject lub mapy od tego. Próbowałem przekształcić to w ciąg i manipulować nim. Ale to nie jest takie proste. Proszę pomóż.

+0

sprawdź moją odpowiedź. To wyjaśnione i działa. –

+0

Użyłem tego Llibrary, http://docs.oracle.com/javaee/7/api/javax/json/ możesz zrobić tak: JsonReader jsonReader = Json.createReader (nowy StringReader (in.toString())); JsonObject jsonObj = jsonReader.readObject(); –

+0

biblioteka javaee w projekcie java se? spróbuj użyć odpowiedniej biblioteki, która pasuje do twoich potrzeb, jak ta napisana powyżej –

Odpowiedz

15

najpierw zrobić to jako ciąg. można użyć niestandardowych Biblioteki za to jak

String message = org.apache.commons.io.IOUtils.toString(rd); 

lub StringBuilder

StringBuilder sb = new StringBuilder(); 

    String line; 
    br = new BufferedReader(new InputStreamReader(is)); 
    while ((line = br.readLine()) != null) { 
     sb.append(line); 
    } 

Następnie można je analizować. Ponieważ jest to obiekt ze względu na "{" (tablica zaczyna się i kończy na []), musisz utworzyć obiekt JSONObject.

JSONObject json = new JSONObject(sb.toString()); 

następnie można uzyskać dostęp do elementów z

//{ "status": "ERROR", "errorCode": "MISSING_PARAMS", "errorMessage": null, "requestId": "20141014181739_11625805172", "downstreamModuleErrorCode": null, "object": [ "activity_code", "activity_name", "points", "frequency", "strategy", "vsa_app_access_token" ]} 

json.getString("status"); 

lub tablicy z

JSONArray jsonArray = new JSONArray(json.getString("object")); 

lub użyć metody getJSONArray()

JSONArray jsonArray = json.getJSONArray("object"); 
+0

Pojawia się komunikat o błędzie: Konstruktor JSONObject (String) jest niezdefiniowany –

+1

Potrzebujesz org.json. Zdobądź go z maven http://mvnrepository.com/artifact/org.json/json lub pobierz bibliotekę/kod przez github. https://github.com/douglascrockford/JSON-java –

0

Używając tej lib (org.json) można przekonwertować ciągi do JSON obiektów: http://www.json.org/java/

JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}"); 
1
InputStream inputStream = connection.getInputStream(); 

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
    StringBuilder builder = new StringBuilder(); 
    String line; 
    while ((line = bufferedReader.readLine()) != null) { 
     builder.append(line + "\n"); 
     } 

JSONArray jsonArray = new JSONArray(builder.toString()); 
for (int i = 0; i < jsonArray.length(); i++) { 
    JSONObject json = jsonArray.getJSONObject(i); 

    if (!json.get("object").equals(null)) { 
     JSONArray objectJsonArray = json.getJSONArray("object"); 
      for (int i = 0; i < objectJsonArray.length(); i++) { 
       JSONObject json = objectJsonArray.getJSONObject(i);  
      }  
    } 
} 

Mam nadzieję, że to pomaga.

+0

dlaczego kopiujesz i wklejasz kod, który nie pasuje do rozwiązania? –