{"id":37022465,"url":"https://github.com/xuanbo/jsonlib","last_synced_at":"2026-01-14T02:41:11.861Z","repository":{"id":57743271,"uuid":"335262695","full_name":"xuanbo/jsonlib","owner":"xuanbo","description":"json 业务库，方便业务统一使用","archived":true,"fork":false,"pushed_at":"2021-02-03T02:13:43.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-08T12:14:48.224Z","etag":null,"topics":["groovy-script","jackson","json-path"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xuanbo.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-02T11:14:40.000Z","updated_at":"2023-01-28T10:13:48.000Z","dependencies_parsed_at":"2022-09-12T10:11:45.085Z","dependency_job_id":null,"html_url":"https://github.com/xuanbo/jsonlib","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/xuanbo/jsonlib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xuanbo%2Fjsonlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xuanbo%2Fjsonlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xuanbo%2Fjsonlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xuanbo%2Fjsonlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xuanbo","download_url":"https://codeload.github.com/xuanbo/jsonlib/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xuanbo%2Fjsonlib/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["groovy-script","jackson","json-path"],"created_at":"2026-01-14T02:41:11.285Z","updated_at":"2026-01-14T02:41:11.850Z","avatar_url":"https://github.com/xuanbo.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonlib\n\n\u003e json 业务库，方便业务统一使用\n\n## 功能\n\n- json 读写（基于 jackson 库）\n- json path (基于 com.jayway.jsonpath 库)\n- json 脚本提取（groovy 脚本）\n\n## 使用\n\n### 依赖\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003etk.fishfish\u003c/groupId\u003e\n    \u003cartifactId\u003ejsonlib\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0.RELEASE\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### json读写\n\n提供 Json 接口进行序列化、反序列化\n\n```java\npackage tk.fishfish.json;\n\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.context.junit4.SpringRunner;\n\nimport java.util.Map;\n\n/**\n * json tests\n *\n * @author 奔波儿灞\n * @version 1.0.0\n */\n@RunWith(SpringRunner.class)\n@SpringBootTest(classes = {\n        JacksonAutoConfiguration.class,\n        JsonConfiguration.class\n})\npublic class JsonTest {\n\n    private final Logger logger = LoggerFactory.getLogger(JsonTest.class);\n\n    @Autowired\n    private Json json;\n\n    @Test\n    public void read() {\n        String json = \"{\\n\" +\n                \"    \\\"store\\\": {\\n\" +\n                \"        \\\"book\\\": [\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"reference\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Nigel Rees\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Sayings of the Century\\\",\\n\" +\n                \"                \\\"price\\\": 8.95\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Evelyn Waugh\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Sword of Honour\\\",\\n\" +\n                \"                \\\"price\\\": 12.99\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Herman Melville\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Moby Dick\\\",\\n\" +\n                \"                \\\"isbn\\\": \\\"0-553-21311-3\\\",\\n\" +\n                \"                \\\"price\\\": 8.99\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"J. R. R. Tolkien\\\",\\n\" +\n                \"                \\\"title\\\": \\\"The Lord of the Rings\\\",\\n\" +\n                \"                \\\"isbn\\\": \\\"0-395-19395-8\\\",\\n\" +\n                \"                \\\"price\\\": 22.99\\n\" +\n                \"            }\\n\" +\n                \"        ],\\n\" +\n                \"        \\\"bicycle\\\": {\\n\" +\n                \"            \\\"color\\\": \\\"red\\\",\\n\" +\n                \"            \\\"price\\\": 19.95\\n\" +\n                \"        }\\n\" +\n                \"    },\\n\" +\n                \"    \\\"expensive\\\": 10\\n\" +\n                \"}\";\n        Map\u003cString, Object\u003e map = this.json.readMap(json);\n        logger.info(\"map: {}\", map);\n    }\n\n}\n```\n\n### jsonpath\n\n当我们对接某些接口时，只需要提取部分数据，此时基于 json path 的 [语法](https://github.com/json-path/JsonPath#operators) 进行数据提取\n\n提供 JsonPath 接口处理 json path\n\n```java\npackage tk.fishfish.json;\n\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.context.junit4.SpringRunner;\n\nimport java.util.List;\nimport java.util.Map;\n\n/**\n * json path tests\n *\n * @author 奔波儿灞\n * @version 1.0.0\n */\n@RunWith(SpringRunner.class)\n@SpringBootTest(classes = {\n        JacksonAutoConfiguration.class,\n        JsonConfiguration.class\n})\npublic class JsonPathTest {\n\n    private final Logger logger = LoggerFactory.getLogger(JsonPathTest.class);\n\n    @Autowired\n    private JsonPath jsonPath;\n\n    @Test\n    public void read() {\n        String json = \"{\\n\" +\n                \"    \\\"store\\\": {\\n\" +\n                \"        \\\"book\\\": [\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"reference\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Nigel Rees\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Sayings of the Century\\\",\\n\" +\n                \"                \\\"price\\\": 8.95\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Evelyn Waugh\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Sword of Honour\\\",\\n\" +\n                \"                \\\"price\\\": 12.99\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Herman Melville\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Moby Dick\\\",\\n\" +\n                \"                \\\"isbn\\\": \\\"0-553-21311-3\\\",\\n\" +\n                \"                \\\"price\\\": 8.99\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"J. R. R. Tolkien\\\",\\n\" +\n                \"                \\\"title\\\": \\\"The Lord of the Rings\\\",\\n\" +\n                \"                \\\"isbn\\\": \\\"0-395-19395-8\\\",\\n\" +\n                \"                \\\"price\\\": 22.99\\n\" +\n                \"            }\\n\" +\n                \"        ],\\n\" +\n                \"        \\\"bicycle\\\": {\\n\" +\n                \"            \\\"color\\\": \\\"red\\\",\\n\" +\n                \"            \\\"price\\\": 19.95\\n\" +\n                \"        }\\n\" +\n                \"    },\\n\" +\n                \"    \\\"expensive\\\": 10\\n\" +\n                \"}\";\n        List\u003cMap\u003cString, Object\u003e\u003e list = jsonPath.readList(json, \"$.store.book\");\n        logger.info(\"list: {}\", list);\n\n        Map\u003cString, Object\u003e map = jsonPath.readMap(json, \"$.store.bicycle\");\n        logger.info(\"map: {}\", map);\n    }\n\n}\n```\n\n### json 脚本提取\n\n某些 api 平台，需要对 json 结果进行转换或封装，则可基于 groovy 脚本进行数据提取与组装\n\n提供 JsonExtractor 接口运行脚本。\n\n```java\npackage tk.fishfish.json;\n\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.context.junit4.SpringRunner;\n\n/**\n * json extractor tests\n *\n * @author 奔波儿灞\n * @version 1.0.0\n */\n@RunWith(SpringRunner.class)\n@SpringBootTest(classes = {\n        JacksonAutoConfiguration.class,\n        JsonConfiguration.class\n})\npublic class JsonExtractorTest {\n\n    private final Logger logger = LoggerFactory.getLogger(JsonExtractorTest.class);\n\n    @Autowired\n    private JsonExtractor jsonExtractor;\n\n    @Test\n    public void extract() {\n        String json = \"{\\n\" +\n                \"    \\\"store\\\": {\\n\" +\n                \"        \\\"book\\\": [\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"reference\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Nigel Rees\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Sayings of the Century\\\",\\n\" +\n                \"                \\\"price\\\": 8.95\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Evelyn Waugh\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Sword of Honour\\\",\\n\" +\n                \"                \\\"price\\\": 12.99\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"Herman Melville\\\",\\n\" +\n                \"                \\\"title\\\": \\\"Moby Dick\\\",\\n\" +\n                \"                \\\"isbn\\\": \\\"0-553-21311-3\\\",\\n\" +\n                \"                \\\"price\\\": 8.99\\n\" +\n                \"            },\\n\" +\n                \"            {\\n\" +\n                \"                \\\"category\\\": \\\"fiction\\\",\\n\" +\n                \"                \\\"author\\\": \\\"J. R. R. Tolkien\\\",\\n\" +\n                \"                \\\"title\\\": \\\"The Lord of the Rings\\\",\\n\" +\n                \"                \\\"isbn\\\": \\\"0-395-19395-8\\\",\\n\" +\n                \"                \\\"price\\\": 22.99\\n\" +\n                \"            }\\n\" +\n                \"        ],\\n\" +\n                \"        \\\"bicycle\\\": {\\n\" +\n                \"            \\\"color\\\": \\\"red\\\",\\n\" +\n                \"            \\\"price\\\": 19.95\\n\" +\n                \"        }\\n\" +\n                \"    },\\n\" +\n                \"    \\\"expensive\\\": 10\\n\" +\n                \"}\";\n        Object result = jsonExtractor.extract(json,\n                \"list = jsonPath.readList(value, '\\\\$.store.book')\\n\" +\n                        \"bicycle = jsonPath.readMap(value, '\\\\$.store.bicycle')\\n\" +\n                        \"list.each{ book -\u003e\\n\" +\n                        \"    book['bicycleColor'] = bicycle.color\\n\" +\n                        \"    book['bicyclePrice'] = bicycle.price\\n\" +\n                        \"}\\n\" +\n                        \"list\");\n        logger.info(\"result: {}\", result);\n    }\n\n}\n```\n\n其中，脚本中默认变量：\n\n- value\n\n  extract 方法传递的 json 字符串\n\n- json\n\n  Json 对象，可进行序列化、反序列化\n\n- jsonPath\n\n  JsonPath 对象，可进行 json path 语法提取\n\n注意：\n\n- 在 groovy 中，$ 是特殊的字符串插值变量，因此需要转义\n\n## 版本\n\n### 1.0.0.RELEASE\n\n依赖：\n\n- Spring Boot 2.3.7.RELEASE\n- Jackson 2.11.3\n- json-path 2.4.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxuanbo%2Fjsonlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxuanbo%2Fjsonlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxuanbo%2Fjsonlib/lists"}