从command.json提取activity字段的Golang代码示例

假设command.json文件里的内容形式如下:

{
 '20230619-091355': 'back|||[0,0][0,0]  activity:com.tencent.mm.plugin.game.luggage.ui.circle.PostCommentDialogUI', 
 '20230619-091426': 'edit||com.tencent.mm:id/cd7|[170,1380][814,1464]  activity:com.tencent.mm.plugin.game.ui.chat_tab.GameWebTabUI', 
 '20230619-091356': 'click|服务|com.tencent.mm:id/ahw|[510,2229][570,2265]  activity:com.tencent.mm.plugin.game.ui.chat_tab.GameWebTabUI'
}

如何用 Golang 从command.json中提取所有的activity:后面的内容内容?

可以使用以下代码来提取所有的activity:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "regexp"
)

type Command struct {
    Activity string `json:"activity"`
}

func main() {
    // 读取command.json文件
    data, err := ioutil.ReadFile("command.json")
    if err != nil {
        panic(err)
    }

    // 解析json
    var commands map[string]string
    err = json.Unmarshal(data, &commands)
    if err != nil {
        panic(err)
    }

    // 提取activity
    activityRegex := regexp.MustCompile(`activity:(.*)`)  // 使用反引号包裹正则表达式
    for _, command := range commands {
        match := activityRegex.FindStringSubmatch(command)
        if len(match) > 1 {
            activity := match[1]
            fmt.Println(activity)
        }
    }
}

该代码会读取command.json文件,解析出所有的命令,并使用正则表达式提取出其中的activity。最终输出所有的activity。

从command.json提取activity字段的Golang代码示例

原文地址: https://www.cveoy.top/t/topic/oI7S 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录