2012-10-13 37 views
3

我的JSON看起來像這 -解析JSON從類HTTPResponse

{"ipinfo": { 
    "ip_address":"4.2.2.2", 
    "ip_type":"Mapped", 

       "Location":{ 

          "continent":"north america", 

          "latitude":33.499, 

          "longitude":-117.662, 

         "CountryData":{ 

           "country":"united states", 

           "country_code":"us"}, 

         "region":"southwest", 

         "StateData":{ 

           "state":"california", 

           "state_code":"ca"}, 

         "CityData":{ 

           "city":"san juan capistrano", 

           "postal_code":"92675", 

           "time_zone":-8}} 

    }} 

這是我下面的代碼,試圖訪問項目的成員在JSONArray

在此拋出
try { 
     String url = service + version + method + ipAddress + format; 
     StringBuilder builder = new StringBuilder(); 
     httpclient = new DefaultHttpClient(); 
     httpget = new HttpGet(url); 
     httpget.getRequestLine(); 
     response = httpclient.execute(httpget); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = entity.getContent(); 
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      for (String line = null; (line = bufferedReader.readLine()) != null;) { 
       builder.append(line).append("\n"); 
      } 
      //Exception getting thrown in below line 
      JSONArray jsonArray = new JSONArray(builder.toString()); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 
      } 
     } 

    } catch (Exception e) { 
     getLogger().log(LogLevel.ERROR, e.getMessage()); 
    } finally { 
     bufferedReader.close(); 
     httpclient.getConnectionManager().shutdown(); 
    } 

我總是收到異常線 -

JSONArray jsonArray = new JSONArray(builder.toString()); 

下面是越來越拋出

異常
org.json.JSONException: A JSONArray text must start with '[' at character 1 

任何人都可以提示我在我的代碼中做了什麼錯?我該如何改進它?

+2

我認爲錯誤信息非常準確。你發佈的JSON沒有數組,只是普通的對象。 JSON語法在http://json.org –

回答

5

我沒有使用過那個特定的API,但是根據這個對象被命名爲JSONArray(關鍵字:數組)的事實來判斷我會猜測它需要一個數組。使用JSON,一個數組必須開始一個[]結束:

[1, 2, 3, 4] 

它可以包含對象:

[{}, {}, {}] 

注意對象如何與{開始,以}結束時,陣列不像:

{ 
    "name": "My Object!" 
} 

由於您的JSON數據看起來更像{object}而不是[array]也許你應該嘗試使用JSONObject來代替。

真的,儘管您有兩種選擇:您可以將JSON數據更改爲數組,或者您可以更改Java代碼以使用JSONObject。 (一個或另一個;不能同時使用。)

更改JSON數據

如作爲在末端添加[在開始和]簡單:

[ 
    { 
     "ipinfo": { 
      "ip_address": "4.2.2.2", 
      "ip_type": "Mapped", 
      "Location": { 
       "continent": "north america", 
       "latitude": 33.499, 
       "longitude": -117.662, 
       "CountryData": { 
        "country": "united states", 
        "country_code": "us" 
       }, 
       "region": "southwest", 
       "StateData": { 
        "state": "california", 
        "state_code": "ca" 
       }, 
       "CityData": { 
        "city": "san juan capistrano", 
        "postal_code": "92675", 
        "time_zone": -8 
       } 
      } 
     } 
    } 
] 

更改Java

最終Java看起來有點像:

// OLD CODE 
//JSONArray jsonArray = new JSONArray(builder.toString()); 
//for (int i = 0; i < jsonArray.length(); i++) { 
// JSONObject jsonObject = jsonArray.getJSONObject(i); 
//} 
// END OLD CODE 
JSONObject jsonObject = new JSONObject(builder.toString()); 

(再次,一個或另一個;不是兩者)。

+1

+1用於概述導致錯誤的期望 – meklarian

+0

@TechGeeky - 出於好奇,您使用了兩種解決方案中的哪一種? –

+0

我選擇了一個JSONObject。我做了正確的選擇嗎? – ferhan

2

您的源代碼JSON只是一個對象。不要加載到數組中,直接加載到JSONObject就足夠了。

JSONObject jsonObject = new JSONObject(builder.toString()); 

該對象將有一個名爲ipinfo的屬性。

+0

+1以最快的速度! –