2015-12-21 68 views
-1

我需要解析JSON。不幸的是,我無法按照here中描述的通常方法從JSON字符串獲取名稱和費率。另外,this one不幫助我。解析沒有數組的JSON

{"usd": 
    {"code":"USD", 
    "alphaCode":"USD", 
    "numericCode":"840", 
    "name":"U.S. Dollar", 
    "rate":1.0857128644692, 
    "date":"Mon, 21 Dec 2015 12:00:01 GMT"}, 
"gbp": 
    {"code":"GBP", 
    "alphaCode":"GBP", 
    "numericCode":"826", 
    "name":"U.K. Pound Sterling", 
    "rate":0.72830809326194, 
    "date":"Mon, 21 Dec 2015 12:00:01 GMT"}, 
    "cad": 
    {"code":"CAD", 
     "alphaCode":"CAD", 
     "numericCode":"124", 
     "name":"Canadian Dollar", 
     "rate":1.5123600265482, 
     "date":"Mon, 21 Dec 2015 12:00:01 GMT"} 
} 

結果:

「美元」, 「1.0857128644692」

「加拿大元」 「1.5123600265482」

+0

請參見本我的回答[鏈接](http://stackoverflow.com/questions/34397172/android-issue-with-jsonarray-parsing)。 我希望你清楚我的步驟。 –

回答

4

嘗試此解決方案

try { 

     JSONObject MainJsonObject = new JSONObject("Your Sting input data"); 

     Iterator keyNames = MainJsonObject.keys(); 

     while (keyNames.hasNext()) { 

      String keyname = (String) keyNames.next(); 

      JSONObject jsonObject = MainJsonObject.getJSONObject(keyname); 

      // parse this this jsonObjectas you required 

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