0

I am trying to retrieve multiple items from the below JSON in the same pass. I wish to retrieve each originatorId and message for each instance of "sequence" into an ordered format so i can display them in a recycler view like a messaging history.

So far i have already tried to use pattern matching via Klaxon but am unable to get it to pick out more than one instance of "message". I think my issue is finding a way to get each instance of the "change" object instead of just the first one e.g=

"$.body.changes.event.message" -> does not work
"$.body.changes[0].event.message" -> returns only first message
{
    "kind": "notification",
    "body":
    {
        "changes": [
                {
                    "sequence": 0,
                    "originatorId": "d5305f84f7812530e746a713ebeabb457e59b380b779387de90ff0f7ff593e2a",
                    "originatorMetadata":
                    {
                        "id": "d5305f84f7812530e746a713ebeabb457e59b380b779387de90ff0f7ff593e2a",
                        "role": "CONSUMER"
                    },
                    "serverTimestamp": 1555330963355,
                    "event":
                    {
                        "type": "ContentEvent",
                        "message": "hello ",
                        "contentType": "text/plain"
                    },
                    "dialogId": "8e35bc87-c42f-4a28-837d-ac6d93cb119a"
                },
                {
                    "sequence": 2,
                    "originatorId": "73c29cd1-b3e3-56fc-a483-ba3409831d21",
                    "originatorMetadata":
                    {
                        "id": "73c29cd1-b3e3-56fc-a483-ba3409831d21",
                        "role": "ASSIGNED_AGENT"
                    },
                    "serverTimestamp": 1555330964870,
                    "event":
                    {
                        "type": "ContentEvent",
                        "message": "Hi Bob, how can I help you today? 😀",
                        "contentType": "text/plain"
                    },
                    "dialogId": "8e35bc87-c42f-4a28-837d-ac6d93cb119a"
                },
                {
                    "sequence": 3,
                    "originatorId": "d5305f84f7812530e746a713ebeabb457e59b380b779387de90ff0f7ff593e2a",
                    "originatorMetadata":
                    {
                        "id": "d5305f84f7812530e746a713ebeabb457e59b380b779387de90ff0f7ff593e2a",
                        "role": "CONSUMER"
                    },
                    "serverTimestamp": 1555330975711,
                    "event":
                    {
                        "type": "ContentEvent",
                        "message": "hi",
                        "contentType": "text/plain"
                    },
                    "dialogId": "8e35bc87-c42f-4a28-837d-ac6d93cb119a"
                }

My expected results would be some form of map with originatorId to message with the mapping position linked to sequence. At the moment i can only get the first message "hello" out and the last originatorId linked together. further work would include getting out the serverTimestamp into the same mapping as well. I am open to using different libraries to Klaxon.

This is the code i have attempted to use already=

 val pathMatcher = object : PathMatcher {
                override fun pathMatches(path: String) = Pattern.matches(".*.message", path)

                override fun onMatch(path: String, value: Any) {

                    val origin = parseForOriginatorId(text)
                    val messageNew = Message(origin, timeReadNew, value.toString())

                    ChatActivity.adapter.addMessage(Message(origin, timeReadNew, value.toString()))
                    //messages[origin] = messageNew

                    Log.d("D", "Message: ${messageNew.originator}: ${messageNew.content}")

                    when (path) {
                        "$.body.changes.originatorId" -> Log.d("D", "======originator ID $value" )
                        "$.body.changes.event.message" -> Log.d("D", "=====Message $value")
                    }
                }
            }

            Klaxon().pathMatcher(pathMatcher)
                    .parseJsonObject(StringReader(text))
        }
4
  • 1
    Have you tried any code? Commented Apr 17, 2019 at 9:42
  • Apologies, question edited Commented Apr 17, 2019 at 9:53
  • Are you familiar with the GSON library? It does all the parsing for you automatically and gives you a Java object where you can access it easily. Check this answer: stackoverflow.com/a/55724722/8086424 Commented Apr 17, 2019 at 9:55
  • GSON helped me a lot, if you submit it as an answer i will accept it Commented Apr 18, 2019 at 8:36

1 Answer 1

1

Loop through the array (an example)

 val myJsonArray = json.get("changes") as JSONArray

 for (i in 0..(myJsonArray.length() - 1)) {
     val item = myJsonArray[i] as JSONObject
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.