如何从json Web服务获取值

时间:2014-05-02 09:17:21

标签: android json

以下是来自网络服务的JSON回复。

{"message":"success","data":[{"push_status":"1"}]}

我想从{"push_status":"1"}获取push_status的值。

这是我的代码:

public static VehicleDetails getPushNotificationstatus(String clientCode, String secretode) throws ClientProtocolException,
            IOException, JSONException {
        Log.d(WebServiceHelper.TAG, "getPushNotificationstatus==============>");
        VehicleDetails vdetails = null;
        String result;
        ArrayList<VehicleDetails> SIArrayList = new ArrayList<VehicleDetails>();
        JSONObject jObject = null;
        try {
            Log.d(WebServiceHelper.TAG, "WebserviceHelperOnLogin>>>>>>>>>>>");
            vdetails = new VehicleDetails();
            METHOD_NAME = "getPushNotifyStatus";
            Log.d(WebServiceHelper.TAG, "URL>>>>>>>>" + URL + METHOD_NAME);
            HttpPost request = new HttpPost(URL + METHOD_NAME);
            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("clientCode", clientCode));
            postParameters.add(new BasicNameValuePair("secretCode", secretode));

            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
                    postParameters);
            request.setEntity(entity);
            HttpResponse response = getThreadSafeClient().execute(request);
            entityResponse = response.getEntity();
            result = EntityUtils.toString(entityResponse, HTTP.UTF_8);
            // Log.i("FB", "result  ::: " + result);
            Log.d(TAG, "ResultPushStatus>>>>" + result);
            jObject = new JSONObject(result);

            vdetails.status_login = jObject.getString("message");
            // Log.i("FB", "contact.status_login  ::: " + contact.status_login);

            if (vdetails.status_login.contentEquals("success")) {
                jObject = new JSONObject(jObject.getString("data"));
                vdetails.pushStatus = jObject.getString("push_status");
                Log.d(TAG, "Push Status==================>" + vdetails.pushStatus);
            } else if (vdetails.status_login.contentEquals("failed")) {
                String Reason = jObject.getString("data").toString();
                Log.d(WebServiceHelper.TAG, "Fail Reason>>>>>" + Reason);
                vdetails.failReason = Reason;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vdetails;
    }

    public static VehicleDetails[] getAllVehicles(String clientCode, String
            secretCode) throws ClientProtocolException, IOException, JSONException {
        VehicleDetails[] vd = null;
        String result = null;

        VehicleDetails vdetails = null;
        ArrayList<VehicleDetails> vehicleArrayList = new ArrayList<VehicleDetails>();
        JSONObject jObject = null;
        String loginUrl = "getAllVehicles";

        try {

            HttpPost request = new HttpPost(URL + loginUrl);
            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

            postParameters.add(new BasicNameValuePair("clientCode", clientCode));
            postParameters.add(new BasicNameValuePair("secretCode", secretCode));

            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(entity);
            HttpResponse response = getThreadSafeClient().execute(request);
            entityResponse = response.getEntity();
            result = EntityUtils.toString(entityResponse, HTTP.UTF_8);
            Log.d(TAG, "result>>" + result);
            JSONObject object = (JSONObject) new JSONTokener(result).nextValue();
            VehicleDetails.status_login = object.getString("message");

            if (VehicleDetails.status_login.contentEquals("success")) {
                JSONArray array = object.getJSONArray("data");
                vehicleArrayList.clear();
                for (int i = 0; i < array.length(); i++) {
                    Log.d(TAG, "LiveTracking>>>>>>>>>>>");

                    JSONObject jObj = array.getJSONObject(i);
                    String vehicleId = jObj.getString("vehicle_id").toString();
                    String vehicleNumber = jObj.getString("vehicle_number").toString();

                    vdetails = new VehicleDetails();

                    vdetails.vehicleId = vehicleId;
                    vdetails.vehicleNo = vehicleNumber;

                    vehicleArrayList.add(vdetails);
                }
                vd = new VehicleDetails[vehicleArrayList.size()];
                for (int x = 0; x < vehicleArrayList.size(); ++x) {
                    vd[x] = (VehicleDetails) vehicleArrayList.get(x);
                }
            } else if (VehicleDetails.status_ login.contentEquals("failed")){

                JSONArray array = object.getJSONArray("data");

                for (int i = 0; i < array.length(); i++) {
                    JSONObject jObj = array.getJSONObject(i);

                    vdetails.failReason = jObj.getString("data").toString();

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return vd;
    }

这是我的日志节目。

    05-02 14:34:40.597: W/System.err(10261): org.json.JSONException: Value [{"push_status":"1"}] of type org.json.JSONArray cannot be converted to JSONObject
    05-02 14:34:40.617: W/System.err(10261):    at org.json.JSON.typeMismatch(JSON.java:107)
    05-02 14:34:40.627: W/System.err(10261):    at org.json.JSONObject.<init>(JSONObject.java:158)
    05-02 14:34:40.627: W/System.err(10261):    at org.json.JSONObject.<init>(JSONObject.java:171)

原因是什么?请帮帮我

2 个答案:

答案 0 :(得分:1)

Response不是JSON对象,它是一个带有一个元素的JSON数组。

JSONArray a = new JSONArray("[{your JSON code}]");
JSONObject = a.getJSONObject(1);

答案 1 :(得分:1)

在以下行中,&#39;数据&#39;返回JSON数组而不是String。 (因为它包含在[])

jObject = new JSONObject(jObject.getString("data"));

所以你需要先获得jsonArray。

JSONArray jArray = jObject.getJSONArray("data");

然后从数组中获取第一个对象。

JSONObject dataObject = jArray.getJSONObject(0);

现在获取String。

String pushStatus = dataObject.getString("push_status");