JSONString不会转换为JSONArray

时间:2015-01-21 18:49:44

标签: android json bluetooth android-bluetooth android-json

我正在尝试通过蓝牙向团队发送一个字符串json到另一个Android设备。由于某种原因,Serializer将加载一小组团队,但是大量的团队列表会导致此错误:

01-21 13:21:30.584    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Unterminated string at character 990 of [{"teleStackDirection":0,"teleCoopSet":0,"autoTotes":0,"teleCoopStack":0,"autoProgs":0,"telePlatform":0,"teleStackTotes":0,"mechLitterPusher":false,"mechLitterInserter":false,"teamNum":0,"mechToteFeeder":false,"teleCarryTote":0,"autoPos":0,"autoMoveTote":false,"mechContainerFlipper":false,"teleHumanStation":0,"id":"6d1d1c80-d386-401c-91c7-3260fdb28a9b","autoMove":false,"telePickUpLitter":false,"autoContainers":0,"teleMoveLitter":false,"teleStackContainers":0,"mechContainerStepRemover":false,"telePlaceTote":0,"telePlaceLitter":0},{"teleStackDirection":0,"teleCoopSet":0,"autoTotes":0,"teleCoopStack":0,"autoProgs":0,"telePlatform":0,"teleStackTotes":0,"mechLitterPusher":false,"mechLitterInserter":false,"teamNum":1,"mechToteFeeder":false,"teleCarryTote":0,"autoPos":0,"autoMoveTote":false,"mechContainerFlipper":false,"teleHumanStation":0,"id":"e10f158f-7254-4bfb-85eb-eb402be790c3","autoMove":false,"telePickUpLitter":false,"autoContainers":0,"teleMoveLitter":false,"teleStackContain
    01-21 13:21:30.618    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value toPos" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.648    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value kContainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.656    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value lse of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.662    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value e of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.728    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value gs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.734    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value gs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.740    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value {"teleCoopSet":0,"teleStackDirection":0,"autoTotes":0,"autoProgs":0,"teleCoopStack":0,"mechLitterPusher":false,"teleStackTotes":0,"telePlatform":0,"mechLitterInserter":false,"teamNum":34,"mechToteFeeder":false,"autoMoveTote":false,"autoPos":0,"teleCarryTote":0,"mechContainerFlipper":false,"teleHumanStation":0,"id":"c2108315-2ea6-4cd5-8205-c845ae48739c","autoMove":false,"autoContainers":0,"telePickUpLitter":false,"teleStackContainers":0,"teleMoveLitter":false,"mechContainerStepRemover":false,"telePlaceLitter":0,"telePlaceTote":0} of type org.json.JSONObject cannot be converted to JSONArray
    01-21 13:21:30.757    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value {"teleCoopSet":0,"teleStackDirection":0,"autoTotes":0,"autoProgs":0,"teleCoopStack":0,"mechLitterPusher":false,"teleStackTotes":0,"telePlatform":0,"mechLitterInserter":false,"teamNum":34,"mechToteFeeder":false,"autoMoveTote":false,"autoPos":0,"teleCarryTote":0,"mechContainerFlipper":false,"teleHumanStation":0,"id":"c2108315-2ea6-4cd5-8205-c845ae48739c","autoMove":false,"autoContainers":0,"telePickUpLitter":false,"teleStackContainers":0,"teleMoveLitter":false,"mechContainerStepRemover":false,"telePlaceLitter":0,"telePlaceTote":0} of type org.json.JSONObject cannot be converted to JSONArray
    01-21 13:21:30.775    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value {"teleCoopSet":0,"teleStackDirection":0,"autoTotes":0,"autoProgs":0,"teleCoopStack":0,"mechLitterPusher":false,"teleStackTotes":0,"telePlatform":0,"mechLitterInserter":false,"teamNum":34,"mechToteFeeder":false,"autoMoveTote":false,"autoPos":0,"teleCarryTote":0,"mechContainerFlipper":false,"teleHumanStation":0,"id":"c2108315-2ea6-4cd5-8205-c845ae48739c","autoMove":false,"autoContainers":0,"telePickUpLitter":false,"teleStackContainers":0,"teleMoveLitter":false,"mechContainerStepRemover":false,"telePlaceLitter":0,"telePlaceTote":0} of type org.json.JSONObject cannot be converted to JSONArray
    01-21 13:21:30.777    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value ners" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.781    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value lePickUpLitter" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.787    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value HumanStation" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.794    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value CarryTote" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.801    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value sher" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.807    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value teleCoopStack" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.866    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value chToteFeeder" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.873    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value ackTotes" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.879    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopSet" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.886    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value ver" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.894    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.900    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.904    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.905    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.908    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.909    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.911    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.912    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.914    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.915    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.916    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.958    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value eStackDirection" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.967    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value kUpLitter" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.974    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Tote" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.981    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.988    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.992    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.994    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:30.995    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.042    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.048    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.053    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.055    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.056    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.058    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.059    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.063    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.065    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.123    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.127    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.133    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.135    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.137    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.139    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:21:31.141    9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
    01-21 13:26:07.836  12185-12185/org.huntingtonrobotics.frcrecyclerushpitscouter I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM build:  (N/A)
        OpenGL ES Shader Compiler Version: E031.24.00.00
        Build Date: 11/12/13 Tue
        Local Branch:
        Remote Branch:
        Local Patches:

这是我发送和阅读字符串的地方:

case Constants.MESSAGE_WRITE:
                    byte[] writeBuf = (byte[]) msg.obj;
                    // construct a string from the buffer
                    String writeMessage = mConversationJSONString;
                    mConversationArrayAdapter.add("Me:  " + writeMessage);
                    break;
                case Constants.MESSAGE_READ:
                    byte[] readBuf = (byte[]) msg.obj;
                    // construct a string from the valid bytes in the buffer
                    String readMessage = new String(readBuf, 0, msg.arg1);
                    //Log.i(TAG, readMessage);
                    try {
                        mSerilizer.saveBluetoothTeams(readMessage);
                    }catch (JSONException je){
                        Log.d(TAG, "Error: " + je);
                    }catch (Exception e){
                        Log.d(TAG, "Error: " + e);
                    }

                    mConversationArrayAdapter.add(mConnectedDeviceName + ":  " + readMessage);
                    break;

这是我的Serializer类的一部分,我将团队保存在发送文件的设备上:

//returns string version of json file
    public String getJSONString() throws JSONException, IOException{
        //Build an array in JSON
        JSONArray array = new JSONArray();
        loadTeams();
        for (Team c : mTeams) {
            array.put(c.toJSON());
        }
        return array.toString();
    }

    //load teams from file system
    public Boolean saveBluetoothTeams(String s) throws IOException, JSONException {
        BufferedReader reader = null;
        try {

            //parse the JSON using JSONTokener

            JSONArray jsonArray = new JSONArray(s);

            ArrayList<Team> bluetoothListData = new ArrayList<Team>();

            //build the array of teams from JSONObjects
            for (int i = 0; i < jsonArray.length(); i++) {
                bluetoothListData.add(new Team(jsonArray.getJSONObject(i)));
            }
            mTeams.addAll(bluetoothListData);
            saveTeams(mTeams);
            return true;
        }catch (FileNotFoundException e) {
            //ignore
            return false;
        }catch (Exception e){
            Log.d(TAG, "Error: " + e);
            return false;

        }finally {
            //ensures the underlying file handle is freed up
            if (reader != null)
                reader.close();
        }
    }

完整蓝牙聊天服务类: https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java

完整蓝牙聊天片段类: https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java

2 个答案:

答案 0 :(得分:1)

问题似乎是您的数据正在分块,并且您正在尝试单独解析这些数据块。

有很多方法可以解决这个问题。一种简单的方法是使用ObjectInputStreamObjectOutputStream代替普通InputStreamOutputStream。对象流变体将处理确保您正在发送和接收完整对象,因此如果您在输出流中发送字符串,则应该在输入流的另一侧返回完整的流。

假设您将JSON作为字符串发送(并且它是UTF-8编码的),发送JSON就像调用writeUTF(String)一样简单,然后使用readUTF()读取它。

答案 1 :(得分:0)

@ Tanis.7x是正确的,我是以块的形式接收我的数据。但是我无法使对象输入/输出流工作,因此我使用了这种方法:

  1. 创建一些随机字符,放在您通过蓝牙发送的数据的末尾。

    //to put on end of string being sent over bluetooth 
    private static final String SUPER_SECRET_PASSWORD = "~~~";
    
  2. 创建一个字符串作为包含所有块的最终字符串

    //for building final string from bluetooth receiver since JSONString was being received in chunks
    private String mFinalString = "";
    
  3. 从蓝牙数据构建字符串,但在其末尾有随机字符之前不要处理它。

              if ((mFinalString.substring(mFinalString.length()-SUPER_SECRET_PASSWORD.length()).equals(SUPER_SECRET_PASSWORD))){
    
                    try {
                        //remove the super secret password from mFinalString
                        mFinalString = mFinalString.substring(0, mFinalString.length()- SUPER_SECRET_PASSWORD.length());
                        //print mFinalString out in the TextView
                        mConversationArrayAdapter.add(mConnectedDeviceName + ":  " + mFinalString);
                        //process mFinalString in the Serializer
                        mSerializer.saveBluetoothTeams(mFinalString);
                        //clear mFinalString
                        mFinalString = "";
                        Toast.makeText(activity, "Teams Received. Please close your app and restart to reload teams.", Toast.LENGTH_LONG).show();
                    }catch (Exception e){
                        Log.e(TAG, "ERROR Saving mFinalString: " + e);
                    }
                }
    
  4. 我的蓝牙片段类的所有代码都可以在这里找到: https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java