How to parse data from pubnub history callback resonse?

Viewed 180

History callback is shown below,I need to parse Object response (message) which response is given below for reference.Object message - params which produce nested array without any keyword and nested object with keyword as message.

pubnub.history(request_id, true, 100, new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
            Log.e(TAG, "successCallback: History Messages" + message);
        }

        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
            Log.e(TAG, "successCallback: History Messages error" + error);
        }
    });

Here is my Object response message.

Response:-

 [                              //array 1
   [                            // array 2
    {                          //obj 1
     "message":{
     "message":"Hai",
     "timestamp":1507105493379,
     "type":"SENT",
     "userId":137
     },
     "timetoken":15071054937865507
     },
     {                           //object 2
     "message":{
     "message":"How are you ?",
     "timestamp":1507105503320,
     "type":"SENT",
     "userId":137
     },
     "timetoken":15071055037143632
     },
     {                                  //object 3
     "message":{
     "message":"Fyn",
     "timestamp":1507105505628,
     "type":"SENT",
     "userId":137
     },
     "timetoken":15071055060355900
     }
     ],                                   //array 1 end
    15071054937865507,
    15071055060355900
  ] 

                                  //array 2 end

How to parse this response.

2 Answers

You can parse your JSON using below code

Call parseJson() inside your successCallback method and pass message.toString() to parse method like this:

public void successCallback(String channel, Object message) {
  super.successCallback(channel, message);
  Log.e(TAG, "successCallback: History Messages" + message);
  parseJson(message.toString());
}

JsonParse method:

private void parseJson(String jsonStr) {
    try{
        JSONArray jsonArray = new JSONArray(jsonStr);
        JSONArray innerJsonArray = jsonArray.getJSONArray(0);
        for(int i = 0; i < innerJsonArray.length(); i++) {
            JSONObject jsonObject = innerJsonArray.getJSONObject(i);
            JSONObject jsonObjectMessage = jsonObject.getJSONObject("message");
            String msg = jsonObjectMessage.getString("message");
            //TODO you can get all other fields 
        }
    }catch (JSONException e){
        e.printStackTrace();
    }
}

first of all this is not a valid JSON, maybe this is way you are having trouble parsing it.

when you'll get a valid json (and you can check if it is a valid json in here https://jsonlint.com/) , you will need to first cast it from a string as a json object and then get every child and every child of a child ans so on until u get the whole object.

you should use some json parser like this one:http://json.parser.online.fr/ to help you understand what object is a child of what

good luck

Related