Android:org.json.jsonobject 值
Android: org.json.jsonobject values
我正在从服务器获取数据并存储在我的 android 应用程序中,但出现异常。
错误消息:无法将 JSONArray 转换为 JSONObject。
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Log.d("response", response.toString());
JSONArray discoverArray = (JSONArray) response.optJSONArray("users");
ArrayList<Discover> discovers = new ArrayList<Discover>();
if (discoverArray != null) {
for (int i = 0; i < discoverArray.length(); i++) {
JSONObject discoverObj = (JSONObject) discoverArray.opt(i);
if (discoverObj != null) {
Discover discover = new Discover();
discover.setAge(discoverObj.optString("age"));
discover.setMutualFriend(discoverObj.optInt("mutualFriend"));
discover.setMutualInterest(discoverObj.optInt("mutualInterest"));
discover.setName(discoverObj.optString("name"));
discovers.add(discover);
}
}
}
Logcat:
org.json.JSONException: Value [{"locationCity":null,"fbCityName":"Százhalombatta","fbCityId":"105979396100282","lastUpdate":"2014-03-05 17:31:13","locale":"hu-HU","locationLongitude":"20","pictures":[{"order":1,"id":false,"url":"https://graph.facebook.com/100001290188792/picture/?width=600&height=600&type=square"}],"lastname":"Ilona","androidPushType":null,"bounce":"0","androidPush":null,"id":"20916","subscribe":"1","locationLatitude":"47","facebook":"100001290188792","name":"Karácsony Ilona","gender":"2","dateRegister":"2013-10-05 22:31:31","orientation":"1","hash":"37e8fd18cb29583879616c47ab87072e","iphonePush":null,"locationRadius":"30","fbLongitude":"18.9333","firstname":"Karácsony","lastLogin":"2014-01-15 10:13:35","email":"cusi28@freemail.hu","iphonePushType":null,"dob":null,"active":"1","fbLatitude":"47.3333","fbAccessToken":"CAAFiaFvj7I0BABZBZBXVwfs6tBSjoIWL133Ehkuzv0y39KziqvfBYYsPs1ep0mb8nHwO6OXw82qwQOusyuO9R5Aq7QcvY4I3xWh0eoxRkp40VZB3ZBITu2Mq5ZAl0RtWLwIdqtIIKiZA7Rh6WJz2cJRvKkHFm9TvzXTrUsVW6HvqeuYpL1q3sk"},{"locationCity":null,"fbCityName":null,"fbCityId":null,"lastUpdate":"2013-11-24 22:33:01","locale":"hu-HU","locationLongitude":"20","pictures":[{"order":1,"id":false,"url":"https://graph.facebook.com/100001346586576/picture/?width=600&height=600&type=square"}],"lastname":"Bognár","androidPushType":null,"bounce":"0","androidPush":null,"id":"20954","subscribe":"1","locationLatitude":"47","facebook":"100001346586576","name":"Szaszy Bognár","gender":"2","dateRegister":"2013-10-06 22:18:14","orientation":"1","hash":"f18cad1db764dd7fe394ac4e4707b84a","iphonePush":null,"locationRadius":"30","fbLongitude":null,"firstname":"Szaszy","lastLogin":"2013-10-06 22:18:14","email":null,"iphonePushType":null,"dob":null,"active":"1","fbLatitude":null,"fbAccessToken":"CAAFiaFvj7I0BAHohcV2ZCbUZC8sx1vhv2ylYBehttW22kmyLz2YdVpoZAabxSQPWPzQwUZAyCQCDRTMxPZCH2buyOkxFrcX7cefZBMUD2rbFZAbLbQZA6gXnHJ4zWJsroh1mKOlnfRjoNkyXTpG4csLzfTfLLydUYHtNKOpAiQa1VLkrtrKFv1dz"},{"locationCity":"2231932","fbCityName":null,"fbCityId":null,"lastUpdate":"2013-11-25 22:33:01","locale":"hu-HU","locationLongitude":"19.08333300000","pictures":[{"order":1,"id":false,"url":"https://graph.facebook.com/559072666/picture/?width=600&height=600&type=square"}],"lastname":"Kerekes","androidPushType":null,"bounce":"0","androidPush":null,"id":"20968","subscribe":"1","locationLatitude":"47.50000000000","facebook":"559072666","name":"Zsofia Kerekes","gender":"2","dateRegister":"2013-10-07 10:47:34","orientation":"1","hash":"4d9eb81eca14f5f362c2813c0c2fe3f3","iphonePush":null,"locationRadius":"10","fbLongitude":null,"firstname":"Zsofia","lastLogin":"2013-10-07 10:47:34","email":"zs.kerekes.13@gmail.com","iphonePushType":null,"dob":null,"active":"1","fbLatitude":null,"fbAccessToken":"CAAFiaFvj7I0BAKCdRjhUiNZBDkWcovRfoEJnKo2pkoYDpnW13PqptEr9FoIeJGLRD1f6dItf9sGhPcReWDdXY3qpEZCPFTp3YK6BOPaDzlY6834PFALImlFOF6uPQBnD2tIC0DYtCZAB1Jvr7jCD6at4MDIi9IHWiEUklj5cFLSzeirYZAhS0JcY7GOi9T0ZD"}] of type org.json.JSONArray cannot be converted to JSONObject
[
{
"active": "1",
"androidPush": null,
"androidPushType": null,
"bounce": "0",
"dateRegister": "2013-10-05 22:31:31",
"dob": null,
"email": "dwdwdwd",
"facebook": "wsda",
"fbAccessToken": "wdwdw",
"fbCityId": "105979396100282",
"fbCityName": "Sz\u00e1zhalombatta",
"fbLatitude": "47.3333",
"fbLongitude": "18.9333",
"firstname": "Kar\u00e1csony",
"gender": "2",
"hash": "wwwwwwwwww",
"id": "20916",
"iphonePush": null,
"iphonePushType": null,
"lastLogin": "2014-01-15 10:13:35",
"lastUpdate": "2014-03-05 17:31:13",
"lastname": "dd",
"locale": "hu-HU",
"locationCity": null,
"locationLatitude": "47",
"locationLongitude": "20",
"locationRadius": "30",
"orientation": "1",
"pictures": [
{
"id": false,
"order": 1,
"url": "dddd"
}
],
"subscribe": "1"
},
{
"active": "1",
"androidPush": null,
"androidPushType": null,
"bounce": "0",
"dateRegister": "2013-10-06 22:18:14",
"dob": null,
"email": null,
"facebook": "ddddd",
"fbAccessToken": "dddwdwdw",
"fbCityId": null,
"fbCityName": null,
"fbLatitude": null,
"fbLongitude": null,
"firstname": "dwdwdw",
"gender": "2",
"hash": "ddwdw",
"id": "20954",
"iphonePush": null,
"iphonePushType": null,
"lastLogin": "2013-10-06 22:18:14",
"lastUpdate": "2013-11-24 22:33:01",
"lastname": "wdwdw",
"locale": "hu-HU",
"locationCity": null,
"locationLatitude": "47",
"locationLongitude": "20",
"locationRadius": "30",
"name": "ddddr",
"orientation": "1",
"pictures": [
{
"id": false,
"order": 1,
"url": "wwwww"
}
],
"subscribe": "1"
},
{
"active": "1",
"androidPush": null,
"androidPushType": null,
"bounce": "0",
"dateRegister": "2013-10-07 10:47:34",
"dob": null,
"email": "asdasd",
"facebook": "ddddd",
"fbAccessToken": "dddd",
"fbCityId": null,
"fbCityName": null,
"fbLatitude": null,
"fbLongitude": null,
"firstname": "ddwwda",
"gender": "2",
"hash": "dwwdwdwdw",
"id": "20968",
"iphonePush": null,
"iphonePushType": null,
"lastLogin": "2013-10-07 10:47:34",
"lastUpdate": "2013-11-25 22:33:01",
"lastname": "ddddd",
"locale": "hu-HU",
"locationCity": "2231932",
"locationLatitude": "47.50000000000",
"locationLongitude": "19.08333300000",
"locationRadius": "10",
"name": "wwwwwww",
"orientation": "1",
"pictures": [
{
"id": false,
"order": 1,
"url": "photo"
}
],
"subscribe": "1"
}
]
非常感谢您的帮助。
替换
JSONObject discoverObj = (JSONObject) discoverArray.opt(i);
与
JSONObject discoverObj = discoverArray.getJSONObject(i);
查看以下代码
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Log.d("response", response.toString());
JSONArray discoverArray = (JSONArray) response.optJSONArray("users");
ArrayList<Discover> discovers = new ArrayList<Discover>();
if (discoverArray != null) {
for (int i = 0; i < discoverArray.length(); i++) {
JSONObject discoverObj = discoverArray.getJSONObject(i);
if (discoverObj != null) {
Discover discover = new Discover();
discover.setAge(discoverObj.optString("age"));
discover.setMutualFriend(discoverObj.optInt("mutualFriend"));
discover.setMutualInterest(discoverObj.optInt("mutualInterest"));
discover.setName(discoverObj.optString("name"));
discovers.add(discover);
}
}
}
我正在从服务器获取数据并存储在我的 android 应用程序中,但出现异常。
错误消息:无法将 JSONArray 转换为 JSONObject。
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Log.d("response", response.toString());
JSONArray discoverArray = (JSONArray) response.optJSONArray("users");
ArrayList<Discover> discovers = new ArrayList<Discover>();
if (discoverArray != null) {
for (int i = 0; i < discoverArray.length(); i++) {
JSONObject discoverObj = (JSONObject) discoverArray.opt(i);
if (discoverObj != null) {
Discover discover = new Discover();
discover.setAge(discoverObj.optString("age"));
discover.setMutualFriend(discoverObj.optInt("mutualFriend"));
discover.setMutualInterest(discoverObj.optInt("mutualInterest"));
discover.setName(discoverObj.optString("name"));
discovers.add(discover);
}
}
}
Logcat:
org.json.JSONException: Value [{"locationCity":null,"fbCityName":"Százhalombatta","fbCityId":"105979396100282","lastUpdate":"2014-03-05 17:31:13","locale":"hu-HU","locationLongitude":"20","pictures":[{"order":1,"id":false,"url":"https://graph.facebook.com/100001290188792/picture/?width=600&height=600&type=square"}],"lastname":"Ilona","androidPushType":null,"bounce":"0","androidPush":null,"id":"20916","subscribe":"1","locationLatitude":"47","facebook":"100001290188792","name":"Karácsony Ilona","gender":"2","dateRegister":"2013-10-05 22:31:31","orientation":"1","hash":"37e8fd18cb29583879616c47ab87072e","iphonePush":null,"locationRadius":"30","fbLongitude":"18.9333","firstname":"Karácsony","lastLogin":"2014-01-15 10:13:35","email":"cusi28@freemail.hu","iphonePushType":null,"dob":null,"active":"1","fbLatitude":"47.3333","fbAccessToken":"CAAFiaFvj7I0BABZBZBXVwfs6tBSjoIWL133Ehkuzv0y39KziqvfBYYsPs1ep0mb8nHwO6OXw82qwQOusyuO9R5Aq7QcvY4I3xWh0eoxRkp40VZB3ZBITu2Mq5ZAl0RtWLwIdqtIIKiZA7Rh6WJz2cJRvKkHFm9TvzXTrUsVW6HvqeuYpL1q3sk"},{"locationCity":null,"fbCityName":null,"fbCityId":null,"lastUpdate":"2013-11-24 22:33:01","locale":"hu-HU","locationLongitude":"20","pictures":[{"order":1,"id":false,"url":"https://graph.facebook.com/100001346586576/picture/?width=600&height=600&type=square"}],"lastname":"Bognár","androidPushType":null,"bounce":"0","androidPush":null,"id":"20954","subscribe":"1","locationLatitude":"47","facebook":"100001346586576","name":"Szaszy Bognár","gender":"2","dateRegister":"2013-10-06 22:18:14","orientation":"1","hash":"f18cad1db764dd7fe394ac4e4707b84a","iphonePush":null,"locationRadius":"30","fbLongitude":null,"firstname":"Szaszy","lastLogin":"2013-10-06 22:18:14","email":null,"iphonePushType":null,"dob":null,"active":"1","fbLatitude":null,"fbAccessToken":"CAAFiaFvj7I0BAHohcV2ZCbUZC8sx1vhv2ylYBehttW22kmyLz2YdVpoZAabxSQPWPzQwUZAyCQCDRTMxPZCH2buyOkxFrcX7cefZBMUD2rbFZAbLbQZA6gXnHJ4zWJsroh1mKOlnfRjoNkyXTpG4csLzfTfLLydUYHtNKOpAiQa1VLkrtrKFv1dz"},{"locationCity":"2231932","fbCityName":null,"fbCityId":null,"lastUpdate":"2013-11-25 22:33:01","locale":"hu-HU","locationLongitude":"19.08333300000","pictures":[{"order":1,"id":false,"url":"https://graph.facebook.com/559072666/picture/?width=600&height=600&type=square"}],"lastname":"Kerekes","androidPushType":null,"bounce":"0","androidPush":null,"id":"20968","subscribe":"1","locationLatitude":"47.50000000000","facebook":"559072666","name":"Zsofia Kerekes","gender":"2","dateRegister":"2013-10-07 10:47:34","orientation":"1","hash":"4d9eb81eca14f5f362c2813c0c2fe3f3","iphonePush":null,"locationRadius":"10","fbLongitude":null,"firstname":"Zsofia","lastLogin":"2013-10-07 10:47:34","email":"zs.kerekes.13@gmail.com","iphonePushType":null,"dob":null,"active":"1","fbLatitude":null,"fbAccessToken":"CAAFiaFvj7I0BAKCdRjhUiNZBDkWcovRfoEJnKo2pkoYDpnW13PqptEr9FoIeJGLRD1f6dItf9sGhPcReWDdXY3qpEZCPFTp3YK6BOPaDzlY6834PFALImlFOF6uPQBnD2tIC0DYtCZAB1Jvr7jCD6at4MDIi9IHWiEUklj5cFLSzeirYZAhS0JcY7GOi9T0ZD"}] of type org.json.JSONArray cannot be converted to JSONObject
[
{
"active": "1",
"androidPush": null,
"androidPushType": null,
"bounce": "0",
"dateRegister": "2013-10-05 22:31:31",
"dob": null,
"email": "dwdwdwd",
"facebook": "wsda",
"fbAccessToken": "wdwdw",
"fbCityId": "105979396100282",
"fbCityName": "Sz\u00e1zhalombatta",
"fbLatitude": "47.3333",
"fbLongitude": "18.9333",
"firstname": "Kar\u00e1csony",
"gender": "2",
"hash": "wwwwwwwwww",
"id": "20916",
"iphonePush": null,
"iphonePushType": null,
"lastLogin": "2014-01-15 10:13:35",
"lastUpdate": "2014-03-05 17:31:13",
"lastname": "dd",
"locale": "hu-HU",
"locationCity": null,
"locationLatitude": "47",
"locationLongitude": "20",
"locationRadius": "30",
"orientation": "1",
"pictures": [
{
"id": false,
"order": 1,
"url": "dddd"
}
],
"subscribe": "1"
},
{
"active": "1",
"androidPush": null,
"androidPushType": null,
"bounce": "0",
"dateRegister": "2013-10-06 22:18:14",
"dob": null,
"email": null,
"facebook": "ddddd",
"fbAccessToken": "dddwdwdw",
"fbCityId": null,
"fbCityName": null,
"fbLatitude": null,
"fbLongitude": null,
"firstname": "dwdwdw",
"gender": "2",
"hash": "ddwdw",
"id": "20954",
"iphonePush": null,
"iphonePushType": null,
"lastLogin": "2013-10-06 22:18:14",
"lastUpdate": "2013-11-24 22:33:01",
"lastname": "wdwdw",
"locale": "hu-HU",
"locationCity": null,
"locationLatitude": "47",
"locationLongitude": "20",
"locationRadius": "30",
"name": "ddddr",
"orientation": "1",
"pictures": [
{
"id": false,
"order": 1,
"url": "wwwww"
}
],
"subscribe": "1"
},
{
"active": "1",
"androidPush": null,
"androidPushType": null,
"bounce": "0",
"dateRegister": "2013-10-07 10:47:34",
"dob": null,
"email": "asdasd",
"facebook": "ddddd",
"fbAccessToken": "dddd",
"fbCityId": null,
"fbCityName": null,
"fbLatitude": null,
"fbLongitude": null,
"firstname": "ddwwda",
"gender": "2",
"hash": "dwwdwdwdw",
"id": "20968",
"iphonePush": null,
"iphonePushType": null,
"lastLogin": "2013-10-07 10:47:34",
"lastUpdate": "2013-11-25 22:33:01",
"lastname": "ddddd",
"locale": "hu-HU",
"locationCity": "2231932",
"locationLatitude": "47.50000000000",
"locationLongitude": "19.08333300000",
"locationRadius": "10",
"name": "wwwwwww",
"orientation": "1",
"pictures": [
{
"id": false,
"order": 1,
"url": "photo"
}
],
"subscribe": "1"
}
]
非常感谢您的帮助。
替换
JSONObject discoverObj = (JSONObject) discoverArray.opt(i);
与
JSONObject discoverObj = discoverArray.getJSONObject(i);
查看以下代码
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Log.d("response", response.toString());
JSONArray discoverArray = (JSONArray) response.optJSONArray("users");
ArrayList<Discover> discovers = new ArrayList<Discover>();
if (discoverArray != null) {
for (int i = 0; i < discoverArray.length(); i++) {
JSONObject discoverObj = discoverArray.getJSONObject(i);
if (discoverObj != null) {
Discover discover = new Discover();
discover.setAge(discoverObj.optString("age"));
discover.setMutualFriend(discoverObj.optInt("mutualFriend"));
discover.setMutualInterest(discoverObj.optInt("mutualInterest"));
discover.setName(discoverObj.optString("name"));
discovers.add(discover);
}
}
}