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;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM