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();
        }
    }
}

 

posted @ 2021-10-11 15:02  力王7314  阅读(256)  评论(0编辑  收藏  举报