2013-03-27 48 views
3

我的問題是一點點不同,我想,我以前問:Parse JSON to cofigure android application 我有json來自服務器,當我看它在瀏覽器源代碼;這是什麼樣子解析JSON,這是在Android應用程序中的HTML中的標籤

JOSON.config:

<html> 
<head></head> 
<body> 
<pre> 
[ 

    { 
     "sett": " ", 
     "glHdr": { 
      "sm": [ ], 
      "scleHPad": false, 
      "st": "sbsm" 
     }, 
     "colrBG": [ 
      23, 
      105, 
      184, 
      100 
     ], 
     "colrTB": [ 
      0, 
      0, 
      0, 
      0 
     ], 
     "colrTR": [ 
      255, 
      255, 
      255, 
      100 
     ], 
     "glFtr": { 
      "icoSz": "icoSzN", 
      "sm": [ ], 
      "scleHPad": false, 
      "gvNR": 3, 
      "gvHIT": false, 
      "gvNC": 3, 
      "st": "igsm" 
     }, 
     "statBr": true 
    }, 
    { 
     "sm": [ 
      { 
       "tbico": "b43-jeep.png", 
       "t": "Welcome!", 
       "w": "http://google.com/start", 
       "st": "w", 
       "wTBL": "wTBLN" 
      }, 
      { 
       "t": "Content screen title", 
       "f": "Eltec%20Spec%20sheet%20Gim%20RD30W.pdf", 
       "st": "f" 
      }, 
      { 
       "tbico": "109-chicken.png", 
       "t": "Sub menu", 
       "sm": [ 
        { 
         "t": "Screen 1", 
         "st": "f" 
        }, 
        { 
         "t": "Screen 2", 
         "w": "Http://google.com", 
         "st": "w", 
         "wTBL": "wTBLT" 
        } 
       ], 
       "st": "sm" 
      }, 
      { 
       "st": "f" 
      } 
     ], 
     "st": "tbm" 
    } 

] 

</pre> 
</body> 
</html> 

功能掃描JSON:

public void doScanAppConfigJson(){ 


      JSONArray appConfig = null; 

      // Function for looping json object via ParseJson class. 
      //Creating JSON Parser instance 
      JSONParser jParser = new JSONParser(); 

      //Getting json strings from url 
      JSONObject jsonObject = jParser.getJSONFromUrl(url); 

     try{ 
      //Getting array of settings 
      appConfig = jsonObject.getJSONArray(ConfigConstants.TABLE_VIEW_SUB_MENU_CONFIG); 
      //loop throw all the objects under -sm[] 
      for (int i = 0; i < appConfig.length(); i++){ 

       JSONObject sm = appConfig.getJSONObject(i); 

       //Now store each of this json in local constant var. 

       String tabTitle = sm.getString(TAG_TITLE); 

       String webAddress = sm.getString(TAG_WEB_ADDRESS); 

       String screenType = sm.getString(TAG_SCREEN_TYPE); 

       String fileName = sm.getString(TAG_FILENAME); 

      } 

     }catch (JSONException e){ 
      e.printStackTrace(); 

     } 

     } 

getJsonFromUrl方法:

公衆的JSONObject getJSONFromUrl(字符串URL){

//Global authentication for link username and password. 
    Authenticator.setDefault(new Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication(){ 

     return new PasswordAuthentication("username", "password".toCharArray()); 
     } 

    }); 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

} 

和錯誤即時得到是:http://cl.ly/image/0z1u2v30341G

現在我猜它的bcz的html文檔類型或別的什麼,但我找不到什麼是錯的。

問:如何解析這個JSON並將其存儲在局部變量中? (我沒有選擇更改服務器端的JSON)在此先感謝。

+2

如果你可以配置服務器停止包裝它在html標籤,這將是理想的。 – 2013-03-27 00:20:27

+0

@SamDufel:我沒有選擇更改服務器端代碼!我可以在應用程序中做些什麼來避免這個標籤並閱讀json? – star18bit 2013-03-27 00:22:36

+0

這和你以前的問題有什麼不同? – cbrulak 2013-03-27 04:50:18

回答

1

那麼你可以提取子字符串,從開放「[」開始,直到關閉「]」,然後分析它作爲JSON,使你的getJSONFromUrl()方法的變化:

public void getJSONFromUrl() { 
    ... 
    ... 

    json = sb.toString().substring(html.indexOf("["), html.lastIndexOf("]") + 1); 

    ... 
}