Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSONException:解析时无法将java.lang.String类型的值转换为JSONObject_Java_Php_Android_Json - Fatal编程技术网

JSONException:解析时无法将java.lang.String类型的值转换为JSONObject

JSONException:解析时无法将java.lang.String类型的值转换为JSONObject,java,php,android,json,Java,Php,Android,Json,因此,当我试图解析JSON对象时,会出现这个错误。我有谷歌的问题,我仍然感到困惑,无法找到解决办法。看起来我的代码是正确的 这是Android Java文件 InputStream is = null; String result = ""; protected String doInBackground(String... params) { try { HttpClient httpclient = new DefaultHttpClient();

因此,当我试图解析JSON对象时,会出现这个错误。我有谷歌的问题,我仍然感到困惑,无法找到解决办法。看起来我的代码是正确的

这是Android Java文件

InputStream is = null;
String result = "";
protected String doInBackground(String... params) {
    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();


        is = entity.getContent();
        Log.v("mysql", "Success");
    } catch (Exception e) {
        Log.v("log_tag", "Error in http connection " + e.toString());
    }
    return null;
}

protected void onPostExecute(String result) {
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
        }
        is.close();

        result=sb.toString();
        Log.v("result", result);
}catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
}

    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","uID: "+json_data.getInt("uID")+
                        ", uName: "+json_data.getString("uName")+
                        ", uPass: "+json_data.getString("uPass")
                );
        }
}catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());

    }

}
InputStream=null;
字符串结果=”;
受保护的字符串doInBackground(字符串…参数){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(参数[0]);
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Log.v(“mysql”,“Success”);
}捕获(例外e){
Log.v(“Log_标记”,“http连接错误”+e.toString());
}
返回null;
}
受保护的void onPostExecute(字符串结果){
试一试{
BufferedReader=新的BufferedReader(新的InputStreamReader(即“UTF-8”));
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
Log.v(“结果”,结果);
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
试一试{
JSONArray jArray=新JSONArray(结果);

对于(int i=0;i您文件中的数据是JSONObject格式的,您正试图以JSONArray格式接收它,这是导致问题的原因。下面这一行是罪魁祸首:

JSONArray jArray = new JSONArray(result);
相反,您需要这样做:

JSONObject jObject = null;
jObject = new JSONObject(mJsonString);
JSONArray jsonImageArray = jObject.getJSONArray("imageTarget");

希望能有所帮助。

关于异常消息,你还不了解什么?你知道JSON格式吗?我知道错误是无法转换的,但我不知道我到底做错了什么以及如何修复它。我知道我正在将其存储到JSONArray中,但我还能存储什么来解析它?你正在尝试将其作为JS进行解析ONArray,当它是一个JSONObject时。mJsonString是否与我的结果字符串等效?您也可以解释一下“imageTarget”。最后,在我编写代码之后,我应该继续执行我的for循环吗?是的,mJsonString与“result”字符串等效。对于imageTarget,我已经附加了我的Json文件。您可以查看它:{“imageTarget”:[“image1”],“videoUrls”:[”“],“videoOrUrl”:[“video”]}谢谢。为了确保我正确理解这一点……对于您提供的JSON文件,我基本上使用jsonImageArray=jObject.getJSONArray(“imageTarget”);后面是jsonImageArray=jObject.getJSONArray(“videoUrls”);等等…?是的,完全正确。只是需要更改变量的名称:)…我使用“JSONIRLARY”而不是“jsonImageArray”来避免过度使用
JSONObject jObject = null;
jObject = new JSONObject(mJsonString);
JSONArray jsonImageArray = jObject.getJSONArray("imageTarget");