【问题标题】:org.json.JSONException: End of input at character 0 of [duplicate]org.json.JSONException:[重复]字符 0 处的输入结束
【发布时间】:2016-04-01 06:48:49
【问题描述】:

在浏览器中手动运行 URL 时,会返回此 JSON。

   {
  "error": false,
  "0": {
    "question": "Using the information that 6.7 × 52 = 348.4, Find the value of: 6.7 × 520",
    "useranswer": "3484",
    "correctanswer": "3484",
    "correct": "1"
  },
  "1": {
    "question": "Jane drives 50mph. London is 350 miles away. How long will it take?",
    "useranswer": "5",
    "correctanswer": "7",
    "correct": "0"
  },
  "2": {
    "question": "74*3?",
    "useranswer": "222",
    "correctanswer": "222",
    "correct": "1"
  },
  "3": {
    "question": "39+31?",
    "useranswer": "70",
    "correctanswer": "70",
    "correct": "1"
  }
}

代码如下:

public List<String> GetTestResultsFromUserID(Integer userID){
    BufferedReader bufferedReader = null;
    try {
        URL url = new URL(AppConfig.Results_URL + "?userid=" + userID);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty ("Authorization", "Basic Z2FycmV0dGg6ZnJBc3Rpbmc0");
        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String result;                    
        result = bufferedReader.readLine();
       return ProcessResultSetFromDatabase(result);
    }catch(Exception e){
        Log.d("Exception in try", "Exception" + e.toString());
        return null;
    }       
}

然后这里处理result

private List<String> ProcessResultSetFromDatabase(String result){
    List<String> resultSet = new ArrayList<String>();
    try{
        JSONObject jObj = new JSONObject(result);
        boolean error = jObj.getBoolean("error");       

        if (!error){ 
            for (int i=0; i<48; i++){
                JSONObject rSet = jObj.getJSONObject(Integer.toString(i));
                resultSet.add("Q: "+rSet.getString("question")+" Correct Ans: "+rSet.getString("correctanswer")+" Given Ans: "+rSet.getString("useranswer")+" Correct:"+(rSet.getString("correct")));                                   
            }     
        }else{
            resultSet.add("No results at the moment");
        }           
    }catch(JSONException e){
        e.printStackTrace();
    }               
    return resultSet;
}

注意:传递给ProcessResultSetFromDatabase的结果在传递时似乎为空。

【问题讨论】:

  • “似乎为空”?你不能检查它是否真的null吗?
  • 最好用Iterator解析
  • @RohitArya 它是result = bufferedReader(new InputStreamReader(con.getInputStream()));,它什么也不返回,只是“”
  • 好的,这意味着网络正在响应空字符串。顺便说一句,“”被称为空而不是null。称它为null 会造成混乱。

标签: java android json


【解决方案1】:

您的 JSON 输出在 "0":{"question": 字符串中,并且您传递整数,因此您需要将 int 转换为字符串。

 for (int i=0; i<48; i++){


int tmpInt = i;
String str= String.valueOf(i);

                JSONObject rSet = jObj.getJSONObject(str);
                resultSet.add("Q: "+rSet.getString("question")+" Correct Ans: "+rSet.getString("correctanswer")+" Given Ans: "+rSet.getString("useranswer")+" Correct:"+(rSet.getString("correct")));                                   
            }  

【讨论】:

    【解决方案2】:

    您应该使用Iterator 解析您的数据。

     JSONObject jObj = new JSONObject(result);
     if (jObj.length() > 0) {
    
                        Iterator<String> keys = jObj.keys();
                        while (keys.hasNext()) {
                            String key = keys.next();
                            JSONObject json= jObj.optJSONObject(key);
    
                            Log.e("json", json.toString());
    
                            if (json!= null) {
                            resultSet.add("Q: "+json.getString("question")+" Correct Ans: "+json.getString("correctanswer")+" Given Ans: "+json.getString("useranswer")+" Correct:"+(json.getString("correct")));                                   
                        }
                    }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多