java - java.lang.String类型的值[{在android中无法转换为JSONObject

标签 java android json exception

我有一个 json wcf 服务,我正在尝试在本地主机模拟器上访问该服务。服务已成功访问,但我的代码中出现奇怪的异常。当我尝试将 json 字符串转换为 json 对象时,我在 logcat 中收到异常。

Value [{ of type java.lang.String cannot be converted to JSONObject 

我从网络服务获取的数据是这样的:

 "[{"message":"Valid user!","status":true}]"

我的代码是这样的:

protected Boolean doInBackground(String... params) {

    String line = "";
    String ur = "http://10.0.2.2:28088/HighriseeSite/appservices.svc/login?username="+etUserName.getText().toString()+"&pass="+etPassword.getText().toString();
    Log.d("STRIMGuuuu",ur);

    try {
        // Replace it with your own WCF service path
        URL json = new URL(ur);
        URLConnection jc = json.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));

        line = reader.readLine();
        String jsonFormattedString = line.replaceAll("\\\\", "");
        Log.d("Json String--->",jsonFormattedString);

        JSONObject jsonmain = new JSONObject(jsonFormattedString);

        if(jsonmain.getBoolean("status")) {
            sharPref = getSharedPreferences("LoginInfo", MODE_PRIVATE);//mode private means that this logininfo cannot be accessed by other apps

            SharedPreferences.Editor editor = sharPref.edit();

            editor.putString("UserName", "sitemanager");
            editor.putString("Password", "admin123$");
            editor.putBoolean("Login", true);
            editor.commit();
        }

        return jsonmain.getBoolean("status");
    }
    catch(Exception e) {
        Log.d("Error--->",e.getMessage());
        return false;
    }
}

logcat 的日志如下:

05-12 18:33:22.336    1874-1900/kanix.highrise.com.highrise D/Json String--->﹕ "[{"message":"Valid user!","status":true}]"
05-12 18:33:22.347    1874-1900/kanix.highrise.com.highrise D/Error--->﹕ Value [{ of type java.lang.String cannot be converted to JSONObject
05-12 18:33:22.354    1874-1895/kanix.highrise.com.highrise W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-12 18:33:22.354    1874-1895/kanix.highrise.com.highrise W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa591ee40, error=EGL_SUCCESS
05-12 18:33:22.383    1874-1886/kanix.highrise.com.highrise I/art﹕ Background sticky concurrent mark sweep GC freed 4572(191KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 1669KB/1884KB, paused 1.143ms total 139.646ms
05-12 18:33:22.706    1874-1895/kanix.highrise.com.highrise W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-12 18:33:22.706    1874-1895/kanix.highrise.com.highrise W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa591ee40, error=EGL_SUCCESS

最佳答案

明白了!需要删除 json 字符串开头和结尾的字符“,如下所示:

  jsonFormattedString= jsonFormattedString.substring(1, jsonFormattedString.length()-1) ;

我的数组现在变成了

 [{"message":"Valid user!","status":true}]

关于java - java.lang.String类型的值[{在android中无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30191910/

相关文章:

javascript - 将 API 返回的 JSON 注入(inject) <li> Materialized.css JS

Java - toString 格式化(格式化 double )

java - 在 Nashorn 中执行 javascript 时找不到文件异常

java - 使用泛型的类调用了错误版本的方法(父类(super class)而不是子类)?

android - 访问为触摸监听器动态创建的层

json - Kafka - 从 JSON 记录到 S3 中的 Parquet 文件

Java - Jersey GET 请求返回 null;等效 URL 在浏览器中产生正确的输出

安卓 map 不显示

android - 如何防止非触摸手机看到Android Market中的应用程序

javascript - 在 AngularJS 中使用 $http.get 检索 JSON 时出现错误 400