如何一次通过JSON对象检索多个项目

时间:2019-04-17 09:39:00

标签: android json parsing kotlin

我试图通过同一遍从下面的JSON检索多个项目。我希望将每个“序列”实例的每个originatorId和消息检索为有序格式,以便我可以像消息传递历史记录一样在回收器视图中显示它们。

到目前为止,我已经尝试通过Klaxon使用模式匹配,但是无法使其选择多个“消息”实例。我认为我的问题是找到一种方法来获取“更改”对象的每个实例,而不仅仅是第一个实例。

"$.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"
                }

我的预期结果将是某种形式的map,带有originatorId到消息的映射位置,该映射位置链接到序列。目前,我只能得到第一个消息“ hello”,而最后一个originatorId链接在一起。 进一步的工作将包括将serverTimestamp移到相同的映射中。 我愿意对Klaxon使用不同的库。

这是我已经尝试使用的代码=

 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))
        }

1 个答案:

答案 0 :(得分:1)

遍历数组(示例)

ffmpeg -i video720x1280.mp4
  -filter_complex
      "[0]scale=hd1080,setsar=1,boxblur=20:20[b];
       [0]scale=-1:1080[v];[b][v]overlay=(W-w)/2"   video1920x1080.mp4