2014-06-23 71 views
-1

我在開發Android應用程序時遇到問題。我的問題是我不知道如何使用GSON解析來自URL的JSON代碼。我搜索了Google和SO大約一個小時左右,但沒有爲我工作。我在互聯網上找到的所有東西都提到自定義的JSON代碼,而不是來自URL的代碼。這是我有的一小部分數據。無法使用GSON解析JSONArray

{ 
    "status": { 
     "error": "NO", 
     "code": 200, 
     "description": "none", 
     "message": "Request ok" 
    }, 
    "geoLocation": { 
     "city_id": "147", 
     "city_long": "Saint-Laurent", 
     "region_short": "QC", 
     "region_long": "Quebec", 
     "country_long": "Canada", 
     "country_id": "43", 
     "region_id": "35" 
    }, 
    "stations": [ 
     { 
      "country": "Canada", 
      "price": "3.65", 
      "address": "3885, Boulevard Saint-Rose", 
      "diesel": "0", 
      "id": "33862", 
      "lat": "45.492367", 
      "lng": "-73.710915", 
      "station": "Shell", 
      "region": "Quebec", 
      "city": "Saint-Laurent", 
      "date": "3 hours agp", 
      "distance": "1.9km" 
     }, 
     { 
      "country": "Canada", 
      "price": "3.67", 
      "address": "3885, Saint-Mary", 
      "diesel": "0", 
      "id": "33872", 
      "lat": "45.492907", 
      "lng": "-73.740715", 
      "station": "Shell", 
      "region": "Quebec", 
      "city": "Saint-Laurent", 
      "date": "3 hours agp", 
      "distance": "2.0km" 
     } 
    ] 
} 

我是JSON/GSON的初學者,所以我需要一些幫助。這裏是我有:

try { 
      String sURL = "http://api.mygasfeed.com/stations/radius/(39.631439)/(-80.8005451)/(25)/reg/(price)/uc82wk25m0.json?callback=?"; 
      URL url = new URL(sURL); 
      HttpURLConnection request = (HttpURLConnection) url.openConnection(); 
      request.connect(); 

      // Convert to a JSON object to print data 
      JsonParser jp = new JsonParser(); //from gson 
      JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //convert the input stream to a json element 
      JsonObject rootobj = root.getAsJsonObject(); //may be an array, may be an object. 
      longitude = rootobj.get("price").getAsString(); 
      latitude = rootobj.get("address").getAsString(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

我試過一個循環來解析數組,但失敗慘了。任何有關這個問題的幫助,高度讚賞。

------------------------------編輯-------------- --------------------------------- 對於不正確的JSON代碼,我感到非常抱歉。我已經更新了代碼,但仍然無法找出解決方案。

+0

您的JSON是不正確檢查「(?」:?http://jsonlint.com/ –

+0

它是怎麼失敗如果你有一個堆棧跟蹤,請添加它。到 –

+0

你的問題使用一些在線JSON驗證 –

回答

1

你JSON是錯誤的:

"date": "3 hours agp", 
    "distance": "1.9km" 
} 
{ 
    "country": "Canada", 

要coorect它,你必須添加一個,

"date": "3 hours agp", 
    "distance": "1.9km" 
}, 
{ 
    "country": "Canada", 
+0

對不起,此只是一個複製錯誤,問題仍然存在,如果你願意,請使用我編寫的代碼中的鏈接。 – user3744439

+0

@ user3744439如果我打開文件,它是一個'?('?應該是什麼? – Jens

+0

嗯。這很有意思休息。我以前沒見過這個。無論如何擺脫它? – user3744439

0

試試這個,使用基本org.json.JSONObjectorg.json .JSONArray它適合我...

// This string is the JSON you gave as imput 
String json = "{ \"status\": { \"error\": \"NO\", \"code\": 200, \"description\": \"none\", \"message\": \"Request ok\" }, \"geoLocation\": { \"city_id\": \"147\", \"city_long\": \"Saint-Laurent\", \"region_short\": \"QC\", \"region_long\": \"Quebec\", \"country_long\": \"Canada\", \"country_id\": \"43\", \"region_id\": \"35\" }, \"stations\": [ {\"country\": \"Canada\",\"price\": \"3.65\",\"address\": \"3885, Boulevard Saint-Rose\",\"diesel\": \"0\",\"id\": \"33862\",\"lat\": \"45.492367\",\"lng\": \"-73.710915\",\"station\": \"Shell\",\"region\": \"Quebec\",\"city\": \"Saint-Laurent\",\"date\": \"3 hours agp\",\"distance\": \"1.9km\" }, {\"country\": \"Canada\",\"price\": \"3.67\",\"address\": \"3885, Saint-Mary\",\"diesel\": \"0\",\"id\": \"33872\",\"lat\": \"45.492907\",\"lng\": \"-73.740715\",\"station\": \"Shell\",\"region\": \"Quebec\",\"city\": \"Saint-Laurent\",\"date\": \"3 hours agp\",\"distance\": \"2.0km\" } ]}"; 

try{ 
    JSONObject rootobj = new JSONObject(json); 
    JSONArray array = rootobj.getJSONArray("stations"); 
    for(int i = 0; i < array.length(); i++){ 
     JSONObject o = array.getJSONObject(i); 
     String price = o.getString("price"); 
     String address = o.getString("address"); 
     //... 

    } 
}catch(JSONException jse){ 
    // Manage Exception here 
} 
0

嘗試下面的代碼從url獲取數據並使用Gson解析響應。

注意:刪除「?callback =?」從您的網址,將刪除您迴應

try { 
    String sURL = "http://api.mygasfeed.com/stations/radius/(39.631439)/(-80.8005451)/(25)/reg/(price)/uc82wk25m0.json"; 

    URL u = new URL(sURL); 
    HttpURLConnection request = (HttpURLConnection) u.openConnection(); 
    request.setRequestMethod("GET"); 
    request.connect(); 
    int status = request.getResponseCode(); 

    switch (status) { 
     case 200: 
     case 201: 
      BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream())); 

      JsonElement element = new Gson().fromJson (br, JsonElement.class); 
      JsonObject jsonObj = element.getAsJsonObject(); 

      JsonArray jArray = jsonObj.get("stations").getAsJsonArray(); 

      for (int i = 0, size = jArray.length(); i < size; i++) { 

       JSONObject jObj = jArray.getJSONObject(i); 

       System.out.println(" Price : " + jObj.get("price").toString()); 
       System.out.println(" Address : " + jObj.get("address").toString()); 
      } 
    } 

} catch (MalformedURLException ex) { 
    // Manage Exception here 
} catch (IOException ex) { 
    // Manage Exception here 
}