当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
19 Star 48 Fork 12

鹞之神乐 / JSONQuery
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

JSONQuery

Maven Central

Easier to use Gson to parse json.

Example:

    @Test
    public void Test() throws TypeNotMismatchException, FieldNotExistException {
        String json = "" +
                "{\n" +
                "  \"errno\": 0,\n" +
                "  \"errmsg\": 成功,\n" +
                "  \"user\": \"{\\\"user_id\\\":643361255,\\\"user_name\\\":\\\"鹞之神乐\\\",\\\"user_sex\\\":1,\\\"user_status\\\":1}\",\n" +
                "  \"comment_info\": [\n" +
                "    {\n" +
                "      \"tid\": \"5504460056\",\n" +
                "      \"pid\": \"116776960983\",\n" +
                "      \"cid\": \"116857893053\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"tid\": \"5504460056\",\n" +
                "      \"pid\": \"116776960983\",\n" +
                "      \"cid\": \"116858057626\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"tid\": \"5504460056\",\n" +
                "      \"pid\": \"116776960983\",\n" +
                "      \"cid\": \"116880757453\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"data\": {\n" +
                "    \"comment_list\": {\n" +
                "      \"116776891765\": {\n" +
                "        \"comment_num\": 3,\n" +
                "        \"comment_list_num\": 4\n" +
                "      },\n" +
                "      \"116776960983\": {\n" +
                "        \"comment_num\": 4,\n" +
                "        \"comment_list_num\": 4\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";

        //获取根元素errno
        JsonResult jsonResult = JSONQuery.select(json, "errno");
        //获取根元素errno,并转换为int
        int errno = jsonResult.getAsInt();
        //获取根元素data中的comment_list
        jsonResult = JSONQuery.select(json, "data > comment_list");
        //正则过滤出属性数组,针对一部分拿对象当数组用的情况
        jsonResult = JSONQuery.select(json, "data > comment_list > [\\d+]");
        //获取数组指定位置的元素
        jsonResult = JSONQuery.select(json, "comment_info > [2]");
        //获取数组指定位置的元素 负数坐标
        jsonResult = JSONQuery.select(json, "comment_info > [-1]");
        //针对某个字符串属性的值又是个json字符串的情况
        jsonResult = JSONQuery.select(json, "user > user_name");
        //jsonResult作为参数替代json字符串
        JsonResult data = JSONQuery.select(json, "data");
        jsonResult = JSONQuery.select(data, "comment_list");
        //将json字符串转换为JsonResult
        jsonResult = JSONQuery.select(json, "");
        jsonResult = JSONQuery.select(json, null);      
        
        // v0.2.5新增
        //将选择结果反序列化为普通对象
        Post post = JSONQuery.select(json, "comment_info > [2]", Post.class);
        //将选择结果反序列化为普通对象数组
        Post[] postArray = JSONQuery.select(json, "comment_info", Post[].class);
        //将选择结果反射为泛型类型List<Post>
        Type type = new TypeToken<List<Post>>() {}.getType();
        List<Post> postList = JSONQuery.select(json, "comment_info", type);
    }

Getting started:

<dependency>
  <!-- JSONQuery @ https://JSONQuery.kagura.me -->
  <groupId>me.kagura</groupId>
  <artifactId>JSONQuery</artifactId>
  <version>0.2.6</version>
</dependency>

Thanks:

Google Gson!

Example web project:

https://github.com/KingFalse/JSONQueryController

Git mirror:

GitHub : https://github.com/KingFalse/JSONQuery

GitEE  : https://gitee.com/Kagura/JSONQuery

Open source

JSONQuery is an open source project distributed under the liberal MIT license. The source code is available at GitHub.

MIT License Copyright (c) 2018 鹞之神乐 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

更简单的使用Gson解析JSON 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/Kagura/JSONQuery.git
git@gitee.com:Kagura/JSONQuery.git
Kagura
JSONQuery
JSONQuery
master

搜索帮助