Android Json分析错误:无法将类型org.Json.JSONArray转换为JSONObject

Android Json分析错误:无法将类型org.Json.JSONArray转换为JSONObject,android,android-json,android-database,jsonparser,Android,Android Json,Android Database,Jsonparser,JSON很简单 [{“千瓦”:“48.90”,“千伏安”:“51.20”,“功率因数”:“-0.96”}] 我得到的错误是 08-26 02:28:49.130 13605-13641/com.whather.emshive E/main活动: url的响应:[{“kw”:“48.90”,“kva”:“51.20”,“pf”:“-0.96”}] Json分析错误:org.Json.JSONArray类型的值[{“kw”:“48.90”,“kva”:“51.20”,“pf”:“-0.96”}] 无

JSON很简单

[{“千瓦”:“48.90”,“千伏安”:“51.20”,“功率因数”:“-0.96”}]

我得到的错误是

08-26 02:28:49.130 13605-13641/com.whather.emshive E/main活动: url的响应:[{“kw”:“48.90”,“kva”:“51.20”,“pf”:“-0.96”}] Json分析错误:org.Json.JSONArray类型的值[{“kw”:“48.90”,“kva”:“51.20”,“pf”:“-0.96”}] 无法转换为JSONObject 08-26 02:28:49.130 13605-13641/com.whatever.emshive/JSON解析器:解析数据时出错 org.json.JSONException:Value org.json.JSONArray类型的[{“kw”:“48.90”,“kva”:“51.20”,“pf”:“-0.96”}] 无法转换为JSONObject

代码是

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

}
public类MainActivity扩展了AppCompatActivity{
私有字符串标记=MainActivity.class.getSimpleName();
私人对话;
私有ListView lv;
//获取联系人JSON的URL
专用静态字符串url=”http://simpleasthat.com/s.php";
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList=新的ArrayList();
lv=(ListView)findViewById(R.id.list);
新建GetContacts().execute();
}
/**
*异步任务类通过HTTP调用获取json
*/
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“contacts”);
//通过所有触点循环
对于(int i=0;ivalue
触点。输入(“kw”,kw);
触点输入(“kva”,kva);
联系方式。put(“pf”,pf);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
MainActivity.this,联系人列表,
R.layout.list_项,新字符串[]{“kw”,“kva”,
“pf”},新的int[]{R.id.kw,
R.id.kva,R.id.pf});
低压设置适配器(适配器);
}
}
}


我试过在StackOverflow中查看其他类似的答案,但没有找到。非常感谢你的帮助。谢谢

正如消息所说,您正在尝试将JSON数组强制转换为JSON对象。您的JSON是一个数组…

请更改GetContacts.doInBackground中的代码

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


contacts
Json键未出现在您发布的Json示例上,请提供完整的Json示例。可能与
JSONArray contacts = new JSONArray(jsonStr);