Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析Android/Java中的JSON对象,获取错误消息org.JSON.JSONException_Java_Android_Jsp - Fatal编程技术网

解析Android/Java中的JSON对象,获取错误消息org.JSON.JSONException

解析Android/Java中的JSON对象,获取错误消息org.JSON.JSONException,java,android,jsp,Java,Android,Jsp,我从服务响应接收了以下JSON格式的数据。我无法解析JSON数据。有谁能帮我找出我犯的错误吗 { "data": { "companyInfo": { "name": "san", "registration_number": "222", "registration_date": null, "address_id": "15", "bank_id":

我从服务响应接收了以下
JSON
格式的数据。我无法解析
JSON
数据。有谁能帮我找出我犯的错误吗

{
    "data": {
        "companyInfo": {
            "name": "san",
            "registration_number": "222",
            "registration_date": null,
            "address_id": "15",
            "bank_id": "34",
            "logo": null,
            "utr_number": null,
            "insurance_number": null,
            "paypal_id": null,
            "active": "yes",
            "created_date": "2015-08-03 16:39:34",
            "company_id": "12",
            "vat_number": null,
            "vat_percent": null,
            "vat_ref_number": null,
            "vat_status": null,
            "vat_date": "0000-00-00 00:00:00",
            "account_number": null,
            "sort_code": null,
            "swift_code": null,
            "iban": null,
            "iban_flag": null,
            "bank_flag": null,
            "bank_name": "san"
        },
        "userInfo": {
            "id": "11",
            "full_name": "san",
            "email": "san@san.com",
            "address_id": "15",
            "company_id": "12",
            "business_category_id": "1",
            "user_type": "0",
            "active": "yes",
            "created_date": "2015-08-03 16:39:34",
            "postal_address": null,
            "street1": "san",
            "street2": null,
            "state": "nepal",
            "zip": null,
            "phone1": "8765",
            "phone2": null,
            "website": null,
            "country": "United States"
        }
    },
    "status": true,
    "message": "Logged in successfully"
}
下面是解析
JSON
对象的java代码

@Override
        protected String doInBackground(String... arg0) {
            String login = "false";
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            url = getResources().getString(R.string.Login_Url);
            //url = getString(R.string.Login_Url);
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST, params);

            Log.d("Response: ", "> " + jsonStr);

                try {

                    JSONObject jsonObj = new JSONObject(jsonStr);
                        JSONObject structure = (JSONObject) jsonObj.get("companyInfo");
                        String a = structure.get("name").toString();

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

            return login;
        }
一、 我收到的错误消息如下所示

org.json.JSONException: No value for companyInfo.

因为在我的响应中有一个名为
companyInfo
的JSON对象。我无法找出我犯的错误在哪里。

还有一个
JSONObject
名为
data
companyInfo
就在里面。你忘了先解析它。因此,解析
数据
,然后解析
公司信息

@Override
        protected String doInBackground(String... arg0) {

            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            url = getResources().getString(R.string.Login_Url);
            //url = getString(R.string.Login_Url);
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST, params);

            Log.d("Response: ", "> " + jsonStr);

                try {

                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONObject data=(JSONObject)jsonObj.get("data");
                        JSONObject structure = (JSONObject) data.get("companyInfo");
                        String a = structure.get("name").toString();

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

            return login;
        }

有点脱离主题,但我建议使用jackson处理json输入/输出:您可以直接从json输入实例化对象或获取对象的json值

ie:Json字符串到对象

// get the json
String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST, params);

// get the object from the json
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonStr, User.class);

// get user.data.companyInfo.name
String name = null;
if(user != null && user.getData() != null && user.getData().getCompanyInfo() != null)
    name = user.getData().getCompanyInfo().getName();

doc:

如果您正在向服务器获取数据,我建议您在onPostExecute方法中执行此操作。