我在开发Android应用程序时遇到了这个熟悉的异常
消息没有值
我确信我的JSONObject包含了我正在寻找的内容:
这是我的JSON对象,您可以对其进行格式化以便更好地理解它:http://jsonformatter.curiousconcept.com/
{
"data": [
{
"id": "1438734076389483_1440132306249660",
"from": {
"category": "Community",
"name": "AKA",
"id": "14387376389483"
},
"message": "TEST TEST",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/14387340769483/posts/14401323069660"
},
{
"name": "Like",
"link": "https://www.facebook.com/14387376389483/posts/14402306249660"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2014-07-14T18:49:43+0000",
"updated_time": "2014-07-14T18:49:43+0000",
"likes": {
"data": [
{
"id": "84207420",
"name": "Akira Yuki"
},
{
"id": "14387346389483",
"name": "AKA"
}
],
"paging": {
"cursors": {
"after": "MTQzODczNDA3NjM4OTQ4Mw==",
"before": "ODQyMDQ3NDIw"
}
}
}
},
{
"id": "1438734076389483_14399988596342",
"from": {
"category": "Community",
"name": "AKA",
"id": "1438734076389483"
},
"message": "MEssage test number twooooo",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/143873076389483/posts/143999881956342"
},
{
"name": "Like",
"link": "https://www.facebook.com/143873076389483/posts/143999819596342"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2014-07-14T10:58:20+0000",
"updated_time": "2014-07-14T10:58:20+0000"
},
{
"id": "143873476389483_1439998362929721",
"from": {
"category": "Community",
"name": "AKA",
"id": "1438734076389483"
},
"message": "Never give up",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/143873406389483/posts/143999836292721"
},
{
"name": "Like",
"link": "https://www.facebook.com/143873076389483/posts/139998362929721"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2014-07-14T10:55:16+0000",
"updated_time": "2014-07-14T10:55:16+0000"
},
{
"id": "1438734076389483_1439998316263059",
"from": {
"category": "Community",
"name": "AKA",
"id": "1438734076389483"
},
"message": "lemme try",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/143873476389483/posts/143998316263059"
},
{
"name": "Like",
"link": "https://www.facebook.com/143734076389483/posts/143998316263059"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2014-07-14T10:54:54+0000",
"updated_time": "2014-07-14T10:54:54+0000"
},
{
"id": "143873407639483_1439990399597184",
"from": {
"category": "Community",
"name": "AKA",
"id": "143873407389483"
},
"message": "AAAAAAAAAAAAAAA BBBBBBBBBBBBBBB",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/143734076389483/posts/143990399597184"
},
{
"name": "Like",
"link": "https://www.facebook.com/143734076389483/posts/143999039957184"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2014-07-14T10:12:51+0000",
"updated_time": "2014-07-14T10:12:51+0000"
},
{
"id": "143873407689483_143998952930603",
"from": {
"category": "Community",
"name": "AKA",
"id": "1438734076389483"
},
"message": "hello",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/143734076389483/posts/14399895420603"
},
{
"name": "Like",
"link": "https://www.facebook.com/143873407638483/posts/143989542930603"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2014-07-14T10:05:09+0000",
"updated_time": "2014-07-14T10:05:09+0000"
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.0/143873476389483/feed?limit=25&since=140363783",
"next": "https://graph.facebook.com/v2.0/143873406389483/feed?limit=25&until=140532308"
}
}
这里是我的代码,因为它是例外
JSONObject oneRowJSONObj = response
.getGraphObject()
.getInnerJSONObject();
JSONArray dataJSONArray = oneRowJSONObj
.getJSONArray("data");
Log.v(TAG, dataJSONArray.toString());
//Working
for (int i = 0; i < dataJSONArray.length() ; i++) {
JSONObject c = dataJSONArray
.getJSONObject(i);
// Storing JSON item in a Variable
String message = c.getString("message");
Log.v("ONE ROW", message);//Here i get the exception
答案 0 :(得分:0)
所以......你正在重复data
部分中的项目:
for (int i = 0; i < dataJSONArray.length() ; i++) {
JSONObject c = dataJSONArray.getJSONObject(i);
但是假设您在该数组中获得的每个项目都是&#34;消息&#34;之一:
String message = c.getString("message");
但不幸的是,它并不仅仅包含它。该数组将包含data
子节中的所有键,包括:id,from,message,actions,privacy等。
因此,您可能(但不能保证)您的第一次迭代会获得只有密钥&#34; id&#34;的JSONObject
。由于您未进行任何检查以查看c
是否有&#34;消息&#34;价值(它没有),你得到了例外。如果密钥不存在,则无法致电getString()
。您可以执行if (c.has("message")
,也可以致电optString()
为其提供可选参数。