2

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.

Raj
  • 103
  • 9
  • Possible duplicate of [How to parse JSON](https://stackoverflow.com/questions/2591098/how-to-parse-json) – Selvin Oct 04 '17 at 10:31

2 Answers2

5

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();
    }
}
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
0

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

Moshe Edri
  • 244
  • 2
  • 10
  • It is valid JSON if you remove the comments that Raj added which he did as a means to make sense of the data. It is not presented this way in the history's response though. – Craig Conover Oct 05 '17 at 23:06