java获取json数组中的值

本文提供了一种简单的方法来解析JSON字符串,并从中提取多级数据。通过三个示例展示了如何获取JSON串中第一、二、三级的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所需jar包的maven依赖:

    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>

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

在这里插入图片描述

一、获取json串的第一级的值。
1.msg为字符串类型的json串,firstKey是需要获取值的名称,如:read、preread
/**
     * Json一级查询
     * @param msg  json转换成字符串类型
     * @param firstKey    要获取值的名称
     * @return
     */
    public static String result(String msg,String firstKey){
        String result;
        try {
            JsonParser jsonParser=new JsonParser();
            JsonObject jsonObject=jsonParser.parse(msg).getAsJsonObject();
            result=jsonObject.get(firstKey).getAsString();

        }catch (Exception e){
            //e.printStackTrace();
            result="-";
        }
        return result;
    }
二、获取json串的第二级的值,如:current、num_procs的值
2.msg为字符串类型的json串,firstKey是需要获取值的名称,如:pids_stats、cpu_stats,而secondKey如:current、system_cpu_usage
/**
     * json二级查询
     * @param msg
     * @param firstKey
     * @param secondKey
     * @return
     */
    public static String result(String msg,String firstKey,String secondKey){
        String result;
        try {
            JSONObject object= JSONObject.fromObject(msg);
            String ob= object.get(firstKey).toString();

            JsonParser jsonParser=new JsonParser();
            JsonObject jsonObject=jsonParser.parse(ob).getAsJsonObject();
            result=jsonObject.get(secondKey).getAsString();
        }catch (Exception e){
           // e.printStackTrace();
            result="-";
        }
        return result;
    }
三、获取json串的第三级的值,如:total_usage、usage_in_usermode的值
3.msg为字符串类型的json串,firstKey是需要获取值的名称,如:pids_stats、cpu_stats,而secondKey如:cpu_usage、throttling_data,thirdKey如:total_usage、throttled_periods
/**
     * json三级查询
     * @param msg
     * @param firstKey
     * @param secondKey
     * @return
     */
    public static String result(String msg,String firstKey,String secondKey,String thirdKey){
        String result;
        try {
            JSONObject object= JSONObject.fromObject(msg);
            String ob= object.get(firstKey).toString();
            JSONObject jsonObject=JSONObject.fromObject(ob);
            String o=jsonObject.get(secondKey).toString();
            JsonParser jsonParser=new JsonParser();
            JsonObject json=jsonParser.parse(o).getAsJsonObject();
            result=json.get(thirdKey).getAsString();
        }catch (Exception e){
            // e.printStackTrace();
            result="-";
        }
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值