duanquanzhi5560 2017-05-25 17:59
浏览 208
已采纳

JSONArray收到错误

org.json.JSONException: Value [{"id":"98747406","name":"adam","surname":"hfdd","age":"2","latitude":"54.2118","longitude":"16.1876","origin":"koszalin","destination":"sian\u00f3w","ride_id":"262243421","date":"2017-05-25 18:13:00"}] at jaPassengers of type java.lang.String cannot be converted to JSONArray

Have anyone idea what cause this error? It occur when I try getJSONArray from server output e.g.:

output:

{"iActualJoinRequests":"4","jaPassengers":"[{\"id\":\"98747406\",\"name\":\"adam\",\"surname\":\"hfdd\",\"age\":\"2\",\"latitude\":\"54.2113448\",\"longitude\":\"16.1876282\",\"origin\":\"koszalin\",\"destination\":\"sian\u00f3w\",\"ride_id\":\"262243421\",\"date\":\"2017-05-25 18:13:00\"}]"}

JSONObject joOutput = new JSONObject(output);
JSONArray jaPassengers = joOutput.getJSONArray("jaPassengers");

EDIT: Problem solved, solution in comment.

  • 写回答

2条回答 默认 最新

  • doufan3408 2017-05-26 21:40
    关注

    Matt Clark, like you advised I'm trying to fix problem on server side. Code:

    $aOutput = array('iActualJoinRequests' => $iActualJoinRequests, 'jaPassengers' => $aPassengers);
    $joOutput = json_encode($aOutput, JSON_FORCE_OBJECT);
    echo $joOutput;
    

    cause error in java when I try to get JSONArray jaPassengers, but:

        $aOutput = array('iActualJoinRequests' => $iActualJoinRequests, 'jaPassengers' => '');
        $joOutput = json_encode($aOutput, JSON_FORCE_OBJECT);
        $joOutput = json_decode($joOutput);
        $joOutput -> jaPassengers = $aPassengers;
        $joOutput = json_encode($joOutput);
        echo $joOutput;
    

    works :)

    Problem solved.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开
  • ¥45 工程软件单片机设计课题要求
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译
  • ¥15 STM32F103上电短路问题
  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框