java - 解析时报错org.json.JSONException : Value at 0 of type java. lang.String cannot be converted to JSONObject

标签 java android android-json

我在解析 json 时遇到这个错误。谁能帮我解决这个问题。

  {
"website": [
    "http://www.example.com",
    "https://buy.example.com"
],
"number": [
    "4546",
    "54256456"
],
"email": [
    "exam@gmail.com",
    "example@gmail.com"
],
"address": [
    "any adddress here",
    "Address 2"
]
  }

任何人都可以帮助我如何准确解析这个 json...

Json解析代码

             String JSON_STRING = response.body().string();
                    Log.e("json_string", JSON_STRING.toString());
                    //Toast.makeText( getApplicationContext(), "Response : "+JSON_STRING, Toast.LENGTH_LONG ).show();

                    JSONObject object = new JSONObject( JSON_STRING );
                    JSONArray jsonArray = object.getJSONArray( "website" );

                        for (int i=0; i<jsonArray.length(); i++)
                        {
                            JSONObject website = jsonArray.getJSONObject( i );
                            Log.e("website", website.toString());
                        }

最佳答案

试试这段代码

 try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray websites = jsonObject.getJSONArray("website");
        for (int i = 0; i < websites.length(); i++) {
            Log.d("TAG", websites.getString(i));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

关于java - 解析时报错org.json.JSONException : Value at 0 of type java. lang.String cannot be converted to JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55515797/

相关文章:

java - SWT.KeyUp 触发过于频繁

android - 如何调整 TableLayout 的 Cells 以适应屏幕?

java - 在 Java 中访问 applicationStorageDirectory

java - 不兼容的类型,发现 List<capture<?仅在 Java 8 中扩展模型>>

java - Java 8 `Stream` 可以在您不要求的情况下并行吗?

java - 在 RestController 方法中获取 ServerWebExchange

android - 现在,Youtube API的配额限制是多少?

java - 将 FirebaseDatabase jsonObject 转换为 jsonArray,然后将 jsonArray 转换为 .xlsx 格式

android - 我正在尝试解析 json 并下载一些 html 而不是 json 内容

android - 数据未进入回收站 View 项目