2016-02-09 329 views
0

我想解析一個JSON,但它給我錯誤Java lang.string不能轉換爲jsonArray。這是我的JSON和錯誤:如何解析json中的對象?

Value {"MemberList":[{"MemberId":1,"FirmId":1,"MemberTypeId":1, 
"MemberUserName":"[email protected]",  "MemberName":"sth","UpdateDate":"\/Date(1421840040000)\/", 
"LastLoginDate":"\/Date(1454995980000)\/", 
"FirmTypeId":1,"FirmName":"Firm","FirmOfficialName":"Firm", 
"FirmRowGuid":"sth","MemberRowGuid":"sth","AuthToken":"sth", 
"IsRegistered":1}],"MemberPageList":null,"FirmModuleList":null,"Status":true, 
"StatusCode":"OK"} of type java.lang.String cannot be converted to JSONArray 

當我刪除會員頭和手動運行工作正常,但我必須用頭解析它,我應該如何對其進行解析。 userDetail是我的JSON字符串:

JSONArray jsonArray = new JSONArray(userDetail); 
        member = new Member(); 
        for (int i = 0; i < jsonArray.length(); i++) { 

        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        member.MemberId = jsonObject.optInt("MemberId"); 
        member.MemberTypeId = jsonObject.optInt("MemberTypeId"); 
        member.FirmTypeId = jsonObject.optInt("FirmTypeId"); 
        member.IsRegistered = jsonObject.optInt("IsRegistered"); 

        member.MemberUserName = jsonObject.optString("MemberUserName"); 
        member.MemberName = jsonObject.optString("MemberName"); 
        member.UpdateDate = jsonObject.optString("UpdateDate"); 
        member.LastLoginDate = jsonObject.optString("LastLoginDate"); 
        member.FirmName = jsonObject.optString("FirmName"); 
        member.FirmOfficialName = jsonObject.optString("FirmOfficialName"); 
        member.FirmRowGuid = jsonObject.optString("FirmRowGuid"); 
        member.MemberRowGuid = jsonObject.optString("MemberRowGuid"); 
        member.AuthToken = jsonObject.optString("AuthToken"); 
       } 

Here is complete code but still not works

+0

是「JSON」的「Value」部分? – Blackbelt

+0

不,這不是它的錯誤,android工作室給我json在MemberList頭開始。 – mesopotamia

+0

解析爲''JSONObject''並從JSON對象中獲取** MemberList **作爲''JSONArray'' – mmuzahid

回答

4

它的JSON對象不是數組,以便改變,

JSONArray jsonArray = new JSONArray(userDetail); 

JSONObject jsonObj= new JSONObject(userDetail); 
JSONArray jsonArray = jsonObj.getJSONArray("MemberList"); 

要手動檢查JSON使用http://jsoneditoronline.org/

CODE:

JSONObject jsonObj= new JSONObject(userDetail); 
    JSONArray jsonArray = jsonObj.getJSONArray("MemberList"); 

        for (int i = 0; i < jsonArray.length(); i++) { 
        member = new Member(); 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        member.MemberId = jsonObject.optInt("MemberId"); 
        member.MemberTypeId = jsonObject.optInt("MemberTypeId"); 
        member.FirmTypeId = jsonObject.optInt("FirmTypeId"); 
        member.IsRegistered = jsonObject.optInt("IsRegistered"); 

        member.MemberUserName = jsonObject.optString("MemberUserName"); 
        member.MemberName = jsonObject.optString("MemberName"); 
        member.UpdateDate = jsonObject.optString("UpdateDate"); 
        member.LastLoginDate = jsonObject.optString("LastLoginDate"); 
        member.FirmName = jsonObject.optString("FirmName"); 
        member.FirmOfficialName = jsonObject.optString("FirmOfficialName"); 
        member.FirmRowGuid = jsonObject.optString("FirmRowGuid"); 
        member.MemberRowGuid = jsonObject.optString("MemberRowGuid"); 
        member.AuthToken = jsonObject.optString("AuthToken"); 
       } 
+0

是的,你的json數組是MemberList不是userDetail,userDetail是json對象,首先得到它的參考並獲取json數組。 –

+0

@Madhur我確實喜歡你說我加了一個ss你能說我做錯了什麼。 – mesopotamia

+0

你得到的響應userDetail是一個jsonobject和json對象,你會得到MemberList作爲jsonarray – Madhur

0
{ 
"MemberList": [{ 
    "MemberId": 1, 
    "FirmId": 1, 
    "MemberTypeId": 1, 
    "MemberUserName": "[email protected]", 
    "MemberName": "sth", 
    "UpdateDate": "\/Date(1421840040000)\/", 
    "LastLoginDate": "\/Date(1454995980000)\/", 
    "FirmTypeId": 1, 
    "FirmName": "Firm", 
    "FirmOfficialName": "Firm", 
    "FirmRowGuid": "sth", 
    "MemberRowGuid": "sth", 
    "AuthToken": "sth", 
    "IsRegistered": 1 
}], 
"MemberPageList": null, 
"FirmModuleList": null, 
"Status": true, 
"StatusCode": "OK" 

}

,你可以看到,我們正在爲對象頂部根數據,這個對象,我們正在「會員」的陣列內,因此您需要首先獲取對象然後將數組放入其中。

,也這樣做是爲了響應在轉換爲String

HttpResponse response=client.execute(post); 
String obj= EntityUtils.toString(response.getEntity()); 

那麼,只要你想使用它。

希望你有這個。