zoukankan      html  css  js  c++  java
  • java解析json字符串

    json字符串内容为:

    {
        "xiaowang": [{
            "age": "21",
            "weight": "120"
        }],
        "xiaomei": [{
            "age": "24",
            "weight": "135"
        }],
        "xiaohong": [{
            "age": "22",
            "weight": "104"
        }]
    }

    java代码为,最后可以得到小王的年纪

    import org.springframework.boot.configurationprocessor.json.JSONArray;
    import org.springframework.boot.configurationprocessor.json.JSONObject;
    
    public class test {
        public static void main(String[] args) {
            String content = "{"xiaowang": [{"age": "21","weight": "120"}],"xiaomei": [{"age": "24","weight": "135"}],"xiaohong": [{"age": "22","weight": "104"}]}";
            try {
                JSONObject jsonObject = new JSONObject(content);
                String xiaowang = jsonObject.getString("xiaowang");
                JSONArray jsonArray = new JSONArray(xiaowang);
                JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                String age = jsonObject1.getString("age");
                System.out.println(age);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    欢迎使用CSDN-markdown编辑器
    欢迎使用CSDN-markdown编辑器
    Math类简介
    Math类简介
    http_server
    tcp服务器
    swoole安装
    laravel源码解析
    VMware的Unity模式
    string.format() %d越界的问题
  • 原文地址:https://www.cnblogs.com/fengzi7314/p/15393428.html
Copyright © 2011-2022 走看看