相关文章推荐
打篮球的匕首  ·  Qt之JSON基础操作_qt ...·  2 周前    · 
潇洒的皮带  ·  Python 操作 Excel 和 ...·  7 月前    · 
火星上的猴子  ·  Android Studio ...·  12 月前    · 
跑龙套的圣诞树  ·  SpringBoot Spring ...·  12 月前    · 
私奔的山楂  ·  Python ...·  1 年前    · 

错误类型JSONArray不能被转换为JSONObject

0 人关注

我正在创建一个应用程序,从网络服务器上获取帖子,我得到一个jsonarray到对象的错误,我是安卓开发和教程的新手,我看到JsonArray之前被命名,所以它会是狗的数组,然后里面会有品种和名称等等,地雷没有命名。

我的代码是

public class GetData extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
    String current = "";
        URL url;
        HttpURLConnection urlConnection = null;
            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) {
        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
    
android
arrays
json
skivecore
skivecore
发布于 2021-04-29
4 个回答
mmfarzaneh
mmfarzaneh
发布于 2021-05-02
已采纳
0 人赞同

You should use like below

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();
    
这很有效,我非常感激
OhhhThatVarun
OhhhThatVarun
发布于 2021-05-02
0 人赞同

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

因此,我认为这将是更合适的。

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);
harikrushna infotech
harikrushna infotech
发布于 2021-05-02
0 人赞同

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

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

2).libraby导入,然后使用

https://www.jsonschema2pojo.org/

对于你的jsonArray到Model类

然后使用jsonArray

Amos Ayomide
Amos Ayomide
发布于 2021-05-02
0 人赞同

用这个代替....

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);