2016-04-15 131 views
-1

我正在使用volley在android中進行網絡調用。我的web服務是給我response對象那樣如何從json對象獲取代碼

{ 
 
   "Code": 200, 
 
   "Message": "Record Found", 
 
   "Result": { 
 
      "deviceid": 10, 
 
      "udid": "359447060356151", 
 
      "businessid": 1, 
 
      "usertype": "Merchant" 
 
   } 
 
}

現在我想讀取該對象的代碼和消息。但沒有這樣做。我正在使用此代碼獲取此數據。

JSONObject jsonObj = new JSONObject(response); 
 
JSONObject userDetails = jsonObj.getJSONObject("Result"); 
 
LoginResponse entryObj = new LoginResponse(); 
 
entryObj.businessid = userDetails.getInt("businessid"); 
 
lr.businessid = entryObj.businessid; 
 
Utilities.businessId = lr.businessid;

幫我讀這個對象的代碼。我沒有得到確切的解決方案。

+0

您是否調試過您是否獲得價值? –

+1

檢查下面的答案。 –

+0

我有upvoted的答案,這是正確的,你的代碼是int,所以你必須使用getInt() – Mohit

回答

0

嘿,你可以使用JSON解析得到的代碼串,

JsonObject jsonObj=new jsonObject(response); 
String code=jsonObj.optString("Code"); 
0

這樣做:

int code = jsonObj.getInt("Code"); 
String message = jsonObj.getString("Message"); 
3
int code = jsonObj.getInt("Code"); 
String message = jsonObj.getString("Message"); 

這是你如何閱讀代碼和消息。