android jsonarray数组转jsonobject异常_Android的,的JSONObject不能转换为JSONArray

In my Android app, I'm trying to communicate with a web server that holds randomly-generated fake usernames and scores.

{ scores: [

{

un: "Feltricapulta",

sc: "143"

},

{

un: "Henroid",

sc: "120"

},

{

un: "ieteubmospta",

sc: "70"

},

{

un: "pmbotesteuai",

sc: "67"

},

{

un: "epesomiubtat",

sc: "65"

}

] }

The code in the PHP file looks like this:

include ('connecttomysql.php');

$command = 'SELECT un, sc FROM xmlscores ORDER BY sc DESC';

$execute_command = mysql_query($command);

echo '{ "scores": ';

while ($table_row = mysql_fetch_assoc($execute_command))

{

$jsonArray [] = $table_row;

}

echo json_encode($jsonArray);

echo '}'

?>

I've called this .php URL in Android using the generic HttpGet method. The output of the json data prints to the stacktrace and provides me with the "Cannot convert from Object to Array" error.

Looking at the PHP file and the json output, is there anything noticeably wrong with my codes or the output? I can't figure it out.

解决方案"Cannot convert from Object to Array"

means you are trying to convert response string to JSONArray. but Current String Contains JSONObject as root element instead of JSONArray. so convert it to JSONObject as:

JSONObject json=new JSONObject();

// get scores JSONArray from json

JSONArray jsonscoresarray=json.getJSONArray("scores");

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值