当前位置: 首页 > 知识库问答 >
问题:

Json解析错误:类型org.Json.jsonArray无法转换为JSONObject

骆嘉石
2023-03-14

JSON非常简单,

public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get contacts JSON
private static String url = "http://simpleasthat.com/s.php";

ArrayList<HashMap<String, String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactList = new ArrayList<>();

    lv = (ListView) findViewById(R.id.list);

    new GetContacts().execute();
}

/**
 * Async task class to get json by making HTTP call
 */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray("contacts");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(0);

                    String kw = c.getString("kw");
                    String kva = c.getString("kva");
                    String pf = c.getString("pf");

                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    // adding each child node to HashMap key => value
                    contact.put("kw", kw);
                    contact.put("kva", kva);
                    contact.put("pf", pf);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                Log.e("JSON Parser", "Error parsing data " + e.toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactList,
                R.layout.list_item, new String[]{"kw", "kva",
                "pf"}, new int[]{R.id.kw,
                R.id.kva, R.id.pf});

        lv.setAdapter(adapter);
    }

}

共有1个答案

班高明
2023-03-14

请从getcontacts.doinbackground中更改代码

JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("contacts");

JSONArray contacts = new JSONArray(jsonStr);
 类似资料:
  • 我是JSON的新手,但我尝试使用所有的答案,但都不起作用。请帮帮我,我做错了什么。 } 我的JSON用于解析。

  • 问题内容: 我是新手,但正在收到以下异常: 在try部分的第一行本身。 请帮助我删除它。这是我的代码: 这是我从我的php文件中获取的json 问题答案: 这个 应该 表示json数组节点 表示json对象节点

  • 我试图实现一个简单的android应用程序,在这个应用程序中,我试图通过php站点检索数据库值,但是下面的代码在解析json数据时产生错误。 我的php文件编码json如下 Android类如下 最后是logcat输出 如何获得表的所需值 请帮忙。

  • 问题内容: 我对Java,eclipse和android还是比较陌生,所以这可能是一个完全愚蠢的问题,但是我仍然要问这个问题。 我有一个正在学习的项目,用于测试连接到flickr api并仅显示最近的图像。我现在要解析从flickr接收到的JSON。我已经下载了gson 1.4,并通过“添加外部jar”将zip添加到了Java构建路径中。它已成功加载,我可以在包资源管理器中的引用库下看到googl

  • 团队, 我试图使用Maven构建一个基本的selenium测试。但是我得到的错误是“FirefoxDriver不能解析为类型”错误。当我尝试通过项目构建路径添加外部JAR时,我没有看到这个错误。只有当我在POM中使用maven依赖项时,我才会看到这个错误。我们非常感谢您的帮助。 我尝试了不同的选择,如只为seleium放1回购等,但没有运气。 在过去的两个星期里,我对这个问题感到震惊。我发现有几个