java 获取json字符串中key对应的值

原文地址:https://blog.csdn.net/weixin_42740530/article/details/88356397

 

用到了Gson的JsonParser

maven项目引入

<dependency>
        <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.3</version>
</dependency>

也可以直接去这个网址下载jar包 https://mvnrepository.com/artifact/com.google.code.gson/gson

使用:

StringBuilder result = {"errcode":40029,"errmsg":"invalid code, hints: [ req_id: GHFDZ44ce-B9Vrda ]"}

 

            JsonParser jp = new JsonParser();
            //将json字符串转化成json对象
            JsonObject jo = jp.parse(result.toString()).getAsJsonObject();
            //获取errcode对应的值
            String errcode = jo.get("errcode").getAsString();
            return errcode;

 

posted @ 2020-08-12 16:17  狴颜丶领衔  阅读(2238)  评论(0编辑  收藏  举报