【问题标题】:java.lang.Integer cannot be converted to JSONObjectjava.lang.Integer 无法转换为 JSONObject
【发布时间】:2015-12-02 16:16:44
【问题描述】:

我正在以 JSON 格式从 PHP 获取状态,但总是得到:

org.json.JSONException: Value 43 of type java.lang.Integer cannot be converted to JSONObject

我从 JSON 读取结果的方式

int strUserID;

......

strUserID = jsonObject.getInt("UserID");

在 php 中我使用的是:

$user_id = mysql_insert_id();
echo $user_id;

if(!$objQuery)
{
    $arr['StatusID'] = "0"; // unable to create user
}
else
{
    $arr['StatusID'] = "1"; // user created successfully
    $arr['UserID'] = $user_id;  // passing user id to android app
}

网络上的 JSON 示例:

 46{"StatusID":"1","UserID":46}

但在 Android 端没有将数据转换为 json 格式,因为面临异常

我可以知道我在哪里做错了吗?

【问题讨论】:

  • 您可以添加您的json 回复吗?
  • 发布您的 Json 数据

标签: java php json


【解决方案1】:

在你的 php 文件中删除

echo $user_id;

在你的 else 条件之后使用它

echo json_encode($arr);

【讨论】:

  • 你是对的 Ravi,user_id 附加在响应数据中。
【解决方案2】:

从 PHP 返回数据时,您应该将其编码为 JSON。 使用下面的函数

回显 json_encode($arr);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多