错误类型 JSONArray 无法转换为 JSONObject

Posted

技术标签:

【中文标题】错误类型 JSONArray 无法转换为 JSONObject【英文标题】:error type JSONArray cannot be converted to JSONObject 【发布时间】:2021-07-22 11:03:13 【问题描述】:

我正在创建一个应用程序来从 Web 服务器获取帖子,并且我收到了一个 jsonarray 到对象错误我是 android 开发和教程的新手,我看到 JsonArray 以前命名过,所以它是数组狗,然后里面会有品种和名字等等,没有命名的地雷。

我的代码是

public class GetData extends AsyncTask<String, String, String>

@Override
protected String doInBackground(String... strings) 
    String current = "";
    try
        URL url;
        HttpURLConnection urlConnection = null;
        try
            url = new URL(JSONURL);
            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream in = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(in);

            int data = isr.read();
            while(data !=-1)
                current += (char) data;
                data = isr.read();
            
            return current;

         catch (MalformedURLException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        
        finally 
            if(urlConnection !=null)
                urlConnection.disconnect();;
            
        
    catch (Exception e)
        e.printStackTrace();
    
    return current;


@Override
protected void onPostExecute(String s) 
    try
        JSONObject jsonObject = new JSONObject(s);
        JSONArray jsonArray = jsonObject.getJSONArray("");
        for(int i = 0; i<jsonArray.length();i++)
            JSONObject jsonObject1= jsonArray.getJSONObject(i);
            namey = jsonObject1.getString("name");
            post = jsonObject1.getString("post");

            //hashmap
            HashMap<String, String> posts = new HashMap<>();
            posts.put("name", namey);
            posts.put("post", post);
            postList.add(posts);

        
     catch (JSONException e) 
        e.printStackTrace();
    

    //Displaying the results
    ListAdapter adapter = new SimpleAdapter(
            MainActivity.this,
            postList,
            R.layout.item,
            new String[]"name", "post",
            new int[]R.id.textView, R.id.textView2);
    lv.setAdapter(adapter);


我要解析的 json 代码是

[
    
        "0": "2",
        "id": "2",
        "1": "anon",
        "name": "anon",
        "2": "goodbye people of skivecore",
        "post": "goodbye people of skivecore",
        "3": "38.751053",
        "lat": "38.751053",
        "4": "-90.432915",
        "lng": "-90.432915",
        "5": "",
        "ip": "",
        "6": "6.204982836749738",
        "distance": "6.204982836749738"
    ,
    
        "0": "1",
        "id": "1",
        "1": "anon",
        "name": "anon",
        "2": "hello people of skivecore",
        "post": "hello people of skivecore",
        "3": "38.744453",
        "lat": "38.744453",
        "4": "-90.607986",
        "lng": "-90.607986",
        "5": "",
        "ip": "",
        "6": "9.280600590285143",
        "distance": "9.280600590285143"
    
]

和堆栈跟踪消息

2021-04-28 23:45:02.156 20352-20352/com.skivecore.secrets W/System.err: org.json.JSONException: Value ["0":"2","id":"2","1":"anon","name":"anon","2":"goodbye people of skivecore","post":"goodbye people of skivecore","3":"38.751053","lat":"38.751053","4":"-90.432915","lng":"-90.432915","5":"","ip":"","6":"6.204982836749738","distance":"6.204982836749738","0":"1","id":"1","1":"anon","name":"anon","2":"hello people of skivecore","post":"hello people of skivecore","3":"38.744453","lat":"38.744453","4":"-90.607986","lng":"-90.607986","5":"","ip":"","6":"9.280600590285143","distance":"9.280600590285143"] of type org.json.JSONArray cannot be converted to JSONObject

【问题讨论】:

【参考方案1】:

你应该像下面这样使用

try 
        JSONArray jsonArray = new JSONArray(s);
        for (int i = 0; i < jsonArray.length(); i++) 
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            namey = jsonObject1.getString("name");
            post = jsonObject1.getString("post");

            //hashmap
            HashMap<String, String> posts = new HashMap<>();
            posts.put("name", namey);
            posts.put("post", post);
            postList.add(posts);

        

     catch (JSONException e) 
        e.printStackTrace();
    

【讨论】:

效果很好,非常感谢!【参考方案2】:

我认为这是 JSONObject jsonObject = new JSONObject(s); 的问题,因为我可以看到您的响应是 JSONArray,因为它的开头和结尾都有 []

所以,我认为这会更合适。

try
        JSONArray jsonArray = jsonObject.getJSONArray(s);
        for(int i = 0; i<jsonArray.length();i++)
            JSONObject jsonObject1= jsonArray.getJSONObject(i);
            namey = jsonObject1.getString("name");
            post = jsonObject1.getString("post");

            //hashmap
            HashMap<String, String> posts = new HashMap<>();
            posts.put("name", namey);
            posts.put("post", post);
            postList.add(posts);

        
    

【讨论】:

【参考方案3】:

请使用 gson 库来使用 jasonArray 和 JsonObject Easley

1)。 https://github.com/google/gson

2)。 libraby 导入然后使用

https://www.jsonschema2pojo.org/

为你的 jsonArray 到 Model 类

然后使用jsonArray

【讨论】:

【参考方案4】:

改用这个....

try 
    JSONArray jsonArray = new JSONArray(s);
    for (int i = 0; i < jsonArray.length(); i++) 
        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
        namey = jsonObject1.getString("name");
        post = jsonObject1.getString("post");

        //put to hashmap
        HashMap<String, String> posts = new HashMap<>();
        posts.put("name", namey);
        posts.put("post", post);
        postList.add(posts);

    

 catch (JSONException e) 
e.printStackTrace();

【讨论】:

以上是关于错误类型 JSONArray 无法转换为 JSONObject的主要内容,如果未能解决你的问题,请参考以下文章

org.json.JSONArray 类型的值无法转换为 JSONObject

org.json.JSONException: org.json.JSONArray 类型的Value[] 无法转换为 JSONObject

org.json.JSONObject 类型的值无法转换为 JSONArray

org.json.JSONArray 类型的无法转换为 JSONObject

react-native android中出现此问题“无法转换类型为org.json.JSONArray的参数”的原因是啥?

org.json.JSONException:java.lang.String 类型的值连接无法转换为 JSONArray