2017-07-29 43 views
0

如何獲得字符串數據我有一個JSON數組像下面並想從形式名字和姓氏的任何形式的幫助,請guzs從JSONObject的

{ 
    "message": "Verification", 
    "data": { 
     "accountStatus": "OPEN", 
     "firstName": "Y", 
     "lastName": "SHITTU", 
     "customerType": "SUD", 
     "invoicePeriod": 1, 
     "dueDate": "2017-09-09T00:00:00+01:00", 
     "customerNumber": 309804811 
    }, 
    "status": "success" 
} 

回答

0
//First create JSON object from the json...  
JSONObject obj = new JSONObject(" .... "); 
//Then get firstname and lastname as string... 
String firstName = obj.getJSONObject("data").getString("firstName"); 
String lastName = obj.getJSONObject("data").getString("lastName"); 
0

我建議你先閱讀JSON解析。剛剛谷歌搜索了這個tutorial

分析器中的問題你的回答: (響應JSON的,你必須爲字符串類型)

JsonObject json = new JsonObject(response); 

    JsonObject data = json.getJsonObject("data"); 

    String firstName = data.getString("firstName"); 
    String lastName = data.getString("lastName"); 
+0

我應該發送一個字符串請求或JSONObject的請求 –

+0

我假設你正在使用抽射問題的標籤。你可以使用任何一個。如果您使用String請求,則必須像我的答案中的第一行一樣創建JsonObject。如果您使用JsonRequest,則可以跳過第一行。 – Bob

+0

你也可以嘗試data.optString(「firstName」); 。當你沒有從jsonObject獲取數據時,optString有助於擺脫空指針異常。 –