0

I want to put the JSON result in textviews but because of multiple array i can get only one key/value of datetime, location and status objects. The json object is:

{  
   "signature":"testSignature",
   "deliverydate":"2015-08-06 15:07:00",
   "datetime":{  
      "0":1438848420,
      "1":1438841820,
      "2":1438838760,
   },
   "location":{  
      "0":"PA",
      "1":"PA",
      "2":"PA",
   },
   "status":{  
      "0":"packed",
      "1":"On the go",
      "2":"delivered",
   },
   "pickupdate":2015-08-04 07:55:00
}

and this is my java code:

try {

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("NO", NUMBER_TO_POST));

    JSONObject json = jsonParser.makeHttpRequest(URL_TO_POST, "POST", params);

    success = json.getString(TAG_SIGNATURE);
    if (success != null) {
        SIGNATURE = json.getString(TAG_SIGNATURE);
        DELIVERY_DATE = json.getString(TAG_DELIVERY_DATE);

        JSONObject DT = json.getJSONObject(TAG_DATETIME);
        DATETIME = DT.getString("0");

        JSONObject LOC = json.getJSONObject(TAG_LOCATION);
        LOCATION = LOC.getString("0");

        JSONObject STAT = json.getJSONObject(TAG_STATUS);
        STATUS = STAT.getString("0");

        PICKUP_DATE = json.getString(TAG_PICKUP_DATE);

    }else{
        finish();

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

can anyone help me to solve this? Thanks

2 Answers 2

1

You should use GSON library to parse JSONs.

And to be a bit more helpful, here is how your class to hold JSON values might look like:

class MyClassForGsonToHoldParseJSON {  
    String signature;
    String deliverydate;
    Map<String, long> datetime;
    Map<String, String> location;
    Map<String, String> status;
    String pickupdate;
}

Then just use something like this to conver variable json with JSON data to an object:

Gson gson = new Gson();
MyClassForGsonToHoldParseJSON f = gson.fromJson(json, MyClassForGsonToHoldParseJSON.class);
Sign up to request clarification or add additional context in comments.

Comments

0

Your JSON format is wrong:

{
    "signature": "testSignature",
    "deliverydate": "2015-08-06 15:07:00",
    "datetime": {
        "0": 1438848420,
        "1": 1438841820,
        "2": 1438838760
    },
    "location": {
        "0": "PA",
        "1": "PA",
        "2": "PA"
    },
    "status": {
        "0": "packed",
        "1": "On the go",
        "2": "delivered"
    },
    "pickupdate": " 2015-08-04 07:55:00"
}

1 Comment

The last sentence should be double quotes Json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.