2015-02-10 171 views
-1

我是Android的初學者如何通過此JSON http://coinabul.com/api.php並顯示在主要活動的文本框中的所有catageory的美元項目,當im顯示日誌usd值顯示210三個鍵,但在json它有三個不同的值無法解析JSON

protected void onPostExecute(InputStream result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 

    progress.dismiss(); 

    String response = ConvertStreamIntoString.convertStreamToString(result); 

    Dictionary<String, Dictionary<String, String>> topDictionary = new Hashtable<String, Dictionary<String, String>>(); 
    Dictionary<String, String> innerDictionary = new Hashtable<String, String>(); 

    try { 
     JSONObject jsobject = new JSONObject(response); 
     Iterator<String> iterator = jsobject.keys(); 
     while (iterator.hasNext()) { 
      String key = iterator.next(); 
      // String value = getString(jsobject, key); 
      String v2=null; 
      JSONObject jsonObject = jsobject.getJSONObject(key); 
      Iterator<String> iterator123 = jsonObject.keys(); 
      while (iterator123.hasNext()) { 
       String keysss = (String) iterator123.next(); 
       String value = getString(jsonObject, keysss); 
       innerDictionary.put(keysss, value); 
       Log.e("innerDict",innerDictionary.toString()); 
      } 

      topDictionary.put(key, innerDictionary); 
      Log.e("asd", topDictionary.toString()); 
     } 


    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    context.showdata(topDictionary, innerDictionary); 
} 
+0

如果我會爲您提供一種方法來解析JSON,那麼它會解決您的問題? – sUndeep 2015-02-10 11:47:15

+0

@sUndeep ok你可以給我的方法 – 2015-02-10 11:59:16

回答

0

請找到下面的方法。你只需要在裏面傳遞響應字符串。請讓我知道如果你發現任何問題:

private void parseResponse(String response) throws JSONException { 
     Dictionary<String, Dictionary<String, String>> topDictionary = new Hashtable<String, Dictionary<String, String>>(); 


     JSONObject jsobject = new JSONObject(response); 
     Iterator<String> iterator = jsobject.keys(); 
     while (iterator.hasNext()) { 
      String key = iterator.next(); 

      JSONObject jsonObject = jsobject.getJSONObject(key); 
      Iterator<String> iterator123 = jsonObject.keys(); 
      Dictionary<String, String> innerDictionary = new Hashtable<String, String>(); 
      while (iterator123.hasNext()) { 
       String keysss = iterator123.next(); 
       String value = jsonObject.getString(keysss); 
       innerDictionary.put(keysss, value); 

      } 
      topDictionary.put(key, innerDictionary); 
     } 
     Log.v("topDictionary",topDictionary.toString());    
     showdata(topDictionary); 
    } 

private void showdata(
      Dictionary<String, Dictionary<String, String>> topDictionary) { 
     for (Enumeration e = topDictionary.keys(); e.hasMoreElements();) { 
      String keyTop = e.nextElement().toString(); 
      Log.v("keyTop",""+keyTop); 
      Dictionary<String, String> innerElements = topDictionary.get(keyTop); 
      for (Enumeration ei = innerElements.keys(); ei.hasMoreElements();) { 
       String keyInner = ei.nextElement().toString(); 
       String value = innerElements.get(keyInner); 
       System.out.println("key: "+keyInner+" value:"+value); 
       // Here you can show your values in textview 
      } 
     } 
    } 
+0

我得到了整個數據,我需要顯示的主要activity..of關鍵名稱「美元」textview上的數據**我通過內部dictonary和外部dictonary **並調用方法showdata(topdictonary,innerdictonary)和主要活動我編寫代碼String s1 = topDictionary.get(innerDictonary).get(「USD」) \t \t textView.setText(s1); 在那個方法,那麼它顯示錯誤PLZ解決它 – 2015-02-10 13:14:31

+0

@Ashutoshsingh你可以請提到你想顯示在textview第1/2/3 /所有人的美元價值? – sUndeep 2015-02-11 06:34:26

+0

所有的美元價值...基本上我想所有的價值有標題美元字符串s1 = topDictionary.get(「銀」)。獲得(「美元」); \t \t String s2 = topDictionary.get(「Gold」)。get(「USD」); \t \t String s3 = topDictionary.get(「BTC」)。get(「USD」); \t \t \t \t String s4 = s1 +「\ n」+ s2 +「\ n」+ s3; \t \t textView.setText(s4);我使用這種方式,但我不想每次提到鑰匙BTC,GOLD,SILVER – 2015-02-11 07:24:05