douyue5856 2018-04-03 14:51
浏览 104

在Android中将PHP响应字符串转换为JSON,总是说JSON字符串是格式错误的

I am trying to send data from MSSQL table on server to Android app via PHP. I am using Volley to send the request for data.

The issue is that no matter what I try, Android says the returned JSON encoded string is "Malformed".

I have put the returned JSON string "response" into online JSON decoders and it displays perfectly, yet Andoid says: "Syntax error, malformed JSON{"data":[{"claimNumber":"f265e5e.....}]".

However, if I hardcode a copy and paste of the returned "response" string into a variable "input" in the Android response function, it works perfectly.

JSONObject jObj = new JSONObject(response); ** Fails - Android says Malformed.
JSONObject jObj = new JSONObject(input);  ** Android uses it with no issues.

The following is the method in Android used to receive the PHP response. Have included Log outputs of both the "response" string and the manually entered "input" string. They are identical, yet Android won't use the actual "response" string.

public void onResponse(String response) {
   if (response.equals("failed")) {
       Log.d("failed", "failed");
       callback.onResponse(null);
       return;
   }
    try {
// Android sees the response returned from the PHP function as always Malformed.
    Log.d("response", response);  
    D/response:  - Syntax error, malformed JSON {"data":[{"claimNumber":"f265e5e6513070abd1f711232cfd3a491075f15bc9714cf723e373f02e71a214","xlsLine":"8b3490231cd923c133989dd93574de131b6c388a86153a74eb6ae2507193a008c9d3d0264452cc1bd1113d5a722f3ea7975f322461f486be96e789424459e20bcbaf878f5d0efe09426b90c046d881100e166ef0bfcf8721266383aabf3837a213854e8425e92b722791640576d3c0ce7722ff3de2e6005c85df840bea3b10982b7bcf8bec3398ea03e08e6a2879cb29e0e6c3c05370e1c2cf819e6156991c762b2247e8a5b51edb852bb371aca011eccaa581ed577e74d6bf0f2a71e43e7751e3fa6e5f68c2a6e4ccd4fc338fc2a2ea4a9d98018c52fe5953741a53d7f7a6f1c1e57901d64c957dc1776ab0ed28f5754f7d44008ebc5864cfd34dd3b1f1bf82db49674b8e7a3e381882f7cc1035bfdba114280f6c173e337d1813fb47a9b1aaf1d52faac197f90cced9eea754cf441cb262b3117444ad3a265cefb9593d40cdb8a3ae0235cc06baffbcac493bf45852908b014f063c29d85347e808e7ed4be8688327bf3280759ea1ff187287dcabaaee8859a6da782cd2451e2911fe16ca7d","markerColor":"0","edited":"0"}]}

// If I copy the returned "response" string manually into a variable "input", Android says it's fine.
    String input = "{\"data\":[{\"claimNumber\":\"f265e5e6513070abd1f711232cfd3a491075f15bc9714cf723e373f02e71a214\",\"xlsLine\":\"8b3490231cd923c133989dd93574de131b6c388a86153a74eb6ae2507193a008c9d3d0264452cc1bd1113d5a722f3ea7975f322461f486be96e789424459e20bcbaf878f5d0efe09426b90c046d881100e166ef0bfcf8721266383aabf3837a213854e8425e92b722791640576d3c0ce7722ff3de2e6005c85df840bea3b10982b7bcf8bec3398ea03e08e6a2879cb29e0e6c3c05370e1c2cf819e6156991c762b2247e8a5b51edb852bb371aca011eccaa581ed577e74d6bf0f2a71e43e7751e3fa6e5f68c2a6e4ccd4fc338fc2a2ea4a9d98018c52fe5953741a53d7f7a6f1c1e57901d64c957dc1776ab0ed28f5754f7d44008ebc5864cfd34dd3b1f1bf82db49674b8e7a3e381882f7cc1035bfdba114280f6c173e337d1813fb47a9b1aaf1d52faac197f90cced9eea754cf441cb262b3117444ad3a265cefb9593d40cdb8a3ae0235cc06baffbcac493bf45852908b014f063c29d85347e808e7ed4be8688327bf3280759ea1ff187287dcabaaee8859a6da782cd2451e2911fe16ca7d\",\"markerColor\":\"0\",\"edited\":\"0\"}]}";

// Log showing Androids interpretation of the manually entered "input" string
// It matches the "response" string exactly yet "response" string fails.

    Log.d("input", input);  
    D/input: {"data":[{"claimNumber":"f265e5e6513070abd1f711232cfd3a491075f15bc9714cf723e373f02e71a214","xlsLine":"8b3490231cd923c133989dd93574de131b6c388a86153a74eb6ae2507193a008c9d3d0264452cc1bd1113d5a722f3ea7975f322461f486be96e789424459e20bcbaf878f5d0efe09426b90c046d881100e166ef0bfcf8721266383aabf3837a213854e8425e92b722791640576d3c0ce7722ff3de2e6005c85df840bea3b10982b7bcf8bec3398ea03e08e6a2879cb29e0e6c3c05370e1c2cf819e6156991c762b2247e8a5b51edb852bb371aca011eccaa581ed577e74d6bf0f2a71e43e7751e3fa6e5f68c2a6e4ccd4fc338fc2a2ea4a9d98018c52fe5953741a53d7f7a6f1c1e57901d64c957dc1776ab0ed28f5754f7d44008ebc5864cfd34dd3b1f1bf82db49674b8e7a3e381882f7cc1035bfdba114280f6c173e337d1813fb47a9b1aaf1d52faac197f90cced9eea754cf441cb262b3117444ad3a265cefb9593d40cdb8a3ae0235cc06baffbcac493bf45852908b014f063c29d85347e808e7ed4be8688327bf3280759ea1ff187287dcabaaee8859a6da782cd2451e2911fe16ca7d","markerColor":"0","edited":"0"}]}

        JSONObject jObj = new JSONObject(input);
        jArray = jObj.getJSONArray("data");
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

// GSON print of JSON Object from the manually entered "input" variable.
I/System.out: {
            "nameValuePairs": {
              "data": {
                "values": [
                  {
                    "nameValuePairs": {
                      "claimNumber": "f265e5e6513070abd1f711232cfd3a491075f15bc9714cf723e373f02e71a214",
                      "xlsLine": "8b3490231cd923c133989dd93574de131b6c388a86153a74eb6ae2507193a008c9d3d0264452cc1bd1113d5a722f3ea7975f322461f486be96e789424459e20bcbaf878f5d0efe09426b90c046d881100e166ef0bfcf8721266383aabf3837a213854e8425e92b722791640576d3c0ce7722ff3de2e6005c85df840bea3b10982b7bcf8bec3398ea03e08e6a2879cb29e0e6c3c05370e1c2cf819e6156991c762b2247e8a5b51edb852bb371aca011eccaa581ed577e74d6bf0f2a71e43e7751e3fa6e5f68c2a6e4ccd4fc338fc2a2ea4a9d98018c52fe5953741a53d7f7a6f1c1e57901d64c957dc1776ab0ed28f5754f7d44008ebc5864cfd34dd3b1f1bf82db49674b8e7a3e381882f7cc1035bfdba114280f6c173e337d1813fb47a9b1aaf1d52faac197f90cced9eea754cf441cb262b3117444ad3a265cefb9593d40cdb8a3ae0235cc06baffbcac493bf45852908b014f063c29d85347e808e7ed4be8688327bf3280759ea1ff187287dcabaaee8859a6da782cd2451e2911fe16ca7d",
                      "markerColor": "0",
                      "edited": "0"
                    }
                  }
                ]
              }
            }
          }

Have tried php output with and without slashes. No change... still malformed. Have tried stripping non-standard chars. There are none. Have forced charset "UTF-8". No difference. Have searched and tried every conceivable solution for this on Google. Nothing works.

I'm at a loss as to what to try next. Totally confused on why Android can't use the returned Json string, but can use it if you manually take the same string and put it in a variable in the code directly.

Can someone tell me if this is a known bug, if the JSON really is malformed or if something else comes to mind?

Thank you

EDIT

Here is the PHP that creates the json to return to Android.

$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt2 = sqlsrv_query( $conn3, $tsql1, $params, $options); 
$row_count = sqlsrv_num_rows($stmt2);
if ($row_count === false) {
    echo("failed");
} else {
    while( $userClaims = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC) ) {
        $claimNum = $userClaims['ClaimNumber'];
        $xls = $userClaims['XLS'];
        $color = $userClaims['Color'];
        $edited = "0";
            $keyvals = (object) array("claimNumber" => $claimNum, "xlsLine" => $xls, "markerColor" => $color, "edited" => $edited);
            array_push($response,$keyvals);
    }

    $encoded = json_encode(array('data' => $response));

EDIT 2

The following is the complete php section of code that creates the json return string. The array is initialized at the top. Have written the php json string to a text file on server and copied the text to a json checker and it works fine.

     $response = array();
        $i = 0;
        $param1 = $token;
        $params = array( &$param1);
        $tsql1 = "SELECT * FROM Claims WHERE Token = ? AND DeletionFlag = 0";
        $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
        $stmt2 = sqlsrv_query( $conn3, $tsql1, $params, $options); 
        $row_count = sqlsrv_num_rows($stmt2);
        if ($row_count === false) {
            echo("failed");
        } else {
            while( $userClaims = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC) ) {
                $claimNum = $userClaims['ClaimNumber'];
                $xls = $userClaims['XLS'];
                $color = $userClaims['Color'];
                $edited = "0";
                    $keyvals = (object) array("claimNumber" => $claimNum, "xlsLine" => $xls, "markerColor" => $color, "edited" => $edited);
                    array_push($response,$keyvals);
            }
            $encoded = json_encode(array('data' => $response));
            echo $encoded;
        }
sqlsrv_close($conn3);
  • 写回答

1条回答 默认 最新

  • dongmaijie5200 2018-04-03 19:13
    关注

    Thank you for your help Andy. I finally figured it out. I found a line of code in the php file that was out of place. It was trying to json_decode an empty string. This is what was generating the extra 36 chars about the malformed string in my actual response. Once I moved it to the correct place, everything started working beautifully. Should have known better, most likely a blasted copy/paste error. Should teach me not to be lazy and actually type ALL my code. Thanks, again!!

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题