2016-11-17 105 views
0

我想獲得給定的Jso值,但「數據」值沒有得到。如何獲得JSON值?

{ 
"result": { 
"success": true, 
"data": { 
    "eid": "mcXRukleFJkd2O2xZvE$5w", 
    "points_earned": 0, 
    "post_sharing_params": { 
    "message": "ajgdakalhajgajabakjkaaahkakakaha. \r\n#LifeAtCapgemini " 
    } 
} 
} 
} 
  • 我創建一個函數來訪問此值,但「數據」 JSON對象值不分配我的模型對象,所以請告訴我,妥善解決。

    try { 
        JSONObject resultJsonObject = new JSONObject(response.toString()); 
        if (resultJsonObject.length() != 0 && resultJsonObject != null) { 
         try { 
          mFeedActionCustomModel.setFeed_success(BaseParser.optString(resultJsonObject, "success")); 
          try { 
           JSONObject dataJsonObject = resultJsonObject.getJSONObject("data"); 
           if (dataJsonObject.length() != 0 && dataJsonObject != null) { 
            mFeedActionCustomModel.setEid(BaseParser.optString(dataJsonObject, "eid")); 
            mFeedActionCustomModel.setPointsEarned(BaseParser.optString(dataJsonObject, "points_earned")); 
            try { 
             JSONObject postSharingParamsJsonObject = dataJsonObject.optJSONObject("post_sharing_params"); 
             if (postSharingParamsJsonObject.length() != 0 && postSharingParamsJsonObject != null) { 
              mFeedActionCustomModel.setFeed_message(BaseParser.optString(postSharingParamsJsonObject, "message")); 
             } 
            } catch (Exception e) { 
            } 
           } 
          } catch (Exception e) { 
          } 
         } catch (Exception e) { 
         } 
        } 
    } catch (Exception e) { 
    } 
    
+0

我建議記錄你的'Exception'(s)而不是完全忽略它們。這樣,您就可以全面瞭解錯誤的根源。 –

回答

1

很簡單:

JSONObject dataJsonObject = resultJsonObject.getJSONObject("result").getJSONObject("data"); 

您還沒有瀏覽過的result屬性還(其中data是孩子)。

+0

解決了我的問題,謝謝 –

+0

@HiteshTarbundiya很樂意提供幫助。如果它幫助你,請不要忘記加註這個答案。 –

+0

我的聲望很低,我不能夠upvoted,但這一個是正確的,爲我工作,再次感謝。 –