2013-09-23 53 views
-1

我試圖解析json時出錯。你能幫我嗎?我閱讀了json url,但是當我試圖解析json時,它給了我一個例外。 代碼:使用android解析Json時出錯

public String lecturaJsonTusPerlas() { 


    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
    HttpPost httppost = new HttpPost("http://www.tvnotas.com.mx/rss/feed/tvn-horoscopo.json"); 
    // Depends on your web service 
    httppost.setHeader("Content-type", "application/json"); 

    InputStream inputStream = null; 
    String result = null; 
    try { 
     HttpResponse response = httpclient.execute(httppost);   
     HttpEntity entity = response.getEntity(); 

     inputStream = entity.getContent(); 
     // json is UTF-8 by default 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
     result = sb.toString(); 
     return result; 

    } catch (Exception e) { 
     Log.d("DEFEKAS","EXCEPCION"); 

    } 
    finally { 
     try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
    } 
    return result; 

閱讀JSON:

 JSONArray jsonArray2 = new JSONArray(lecturaJsonTusPerlas); 

     for (int i = 0; i < jsonArray2.length(); i++) { 
      JSONObject jsonObject = jsonArray2.getJSONObject(i); 

     String atr1 = null; 
     boolean var1 = jsonObject.getBoolean(atr1); 
     String atr2 = null; 
     int var2 = jsonObject.getInt(atr2); 
     String atr4 = null; 
     String var3 = jsonObject.getString(atr4); 
     } 

我的事情,網址是JSON,因爲當我試圖與谷歌瀏覽器擴展我沒有任何問題的提取。

+0

你得到的是一個JSONObject 'JSONObject jb = new JSONObject(lecturaJsonTusPerlas)' – Raghunandan

+0

請發佈您的堆棧跟蹤 – Jon

回答

1

試試這個

JSONObject jObject = new JSONObject(lecturaJsonTusPerlas); 

JSONArray jsonArray2 = jObject.getJSONArray("your array key"); 

     for (int i = 0; i < jsonArray2.length(); i++) { 
      JSONObject jsonObject = jsonArray2.getJSONObject(i); 

boolean var1 = jsonObject.getBoolean(atr1); 
int var2 = jsonObject.getInt(atr2); 
String var3 = jsonObject.getString(atr4); 

} 

不初始化字符串值作爲空。

1

你可能會得到NullPointerException異常,因爲這些線路:

String atr1 = null; 
boolean var1 = jsonObject.getBoolean(atr1); 
String atr2 = null; 
int var2 = jsonObject.getInt(atr2); 
String atr4 = null; 
String var3 = jsonObject.getString(atr4); 

你想用「空」參考解析。 請參閱atr1,atr2和atr4;這些字符串用「null」初始化。

0

你得到這個錯誤的原因是你的json元素的頂層節點是對象而不是JSON數組。

於是開始分析你需要寫這樣的事情

JSONObject jObject = new JSONObject(result); 
2

你得到的是一個JSONObject

JSONObject jb = new JSONObject(lecturaJsonTusPerlas()); 

您的JSON看起來像下面的鏈接已經使用

{ // jsonobject node 
    "@attributes": { 
     "version": "2.0" 
    }, 
    "channel": { 
     "title": "TV Notas", 
     "link": "http://www.tvnotas.com.mx", 
     "description": "TV Notas - Horoscopos", 
     "pubDate": "Mon, 23 Sep 2013 2:30:12 -0500", 
     "generator": "http://www.tvnotas.com.mx", 
     "language": "es", 
     "item": [ // json array of item's 
      { 
       "title": "Acuario", 
       "link": "http://usa.tvnotas.com.mx/horoscopo/1-acuario/", 
       "pubDate": "Mon, 23 Sep 2013 02:30:12 -0500", 
       "category": "Horoscopo", 
       "guid": "http://www.tvnotas.com.mx/horoscopo/1-acuario/", 
       "description": "Si has sido soberbio con tus compañeros de trabajo o empleados, con familiares o amigos y no les has perdonado sus malos momentos, ahora se te presentará la oportunidad de estrechar lazos.", 
       "enclosure": { 
        "@attributes": { 
         "url": "http://www.tvnotas.com.mx/advf/imagenes/2013/01/50fdbd604f653_150x118.jpg", 
         "length": "3587", 
         "type": "image/jpeg" 
        } 
       }, 
       "elemento": "Aire", 
       "planeta": "Urano", 
       "signo_compatible": "Cáncer", 
       "signo_enemigo": "El excéntrico", 
       "numero": "25", 
       "arquetipo": "El Loco Sabio", 
       "arcangel": "Sakmakrel", 
       "color": "Verde", 
       "dia_suerte": "28" 
      }, 
      .... 

爲了解析

try { 
     JSONObject jb = new JSONObject(lecturaJsonTusPerlas()); 
     JSONObject job = jb.getJSONObject("channel"); 
     String channeltitle= job.getString("title"); 
     String channellink= job.getString("link"); 
     String channeldecription= job.getString("description"); 
     String channeldpubdate= job.getString("pubDate"); 
     String channeldgenerator = job.getString("generator"); 
     String channeldlanguage = job.getString("language"); 
     JSONArray jr = job.getJSONArray("item"); 

     for(int i=0;i<jr.length();i++) 
     { 
      JSONObject jb1 = (JSONObject) jr.get(i); 
      String title = jb1.getString("title"); 
          // similar for title link and others 
      JSONObject enclosure = jb1.getJSONObject("enclosure"); 
      JSONObject attributes= enclosure.getJSONObject("@attributes"); 
      String url = attributes.getString("url"); 
      Log.i("....",""+title); 
      String elemento= jb1.getString("elemento"); 
      // similar for others planeta.. 
     } 

     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
+0

不能簡單,那麼這個。 :) – Deepak

+0

@DeepakMalik謝謝。很高興聽到並希望它是有幫助的 – Raghunandan