【Java】java获取json中某个字段
import com.alibaba.fastjson.JSONObject; public class JsonTest { public static void main(String[] args) { // json串(以自己的为准) String str = "{"id":"75","shoppingCartItemList":[{"id":"407","num":"10"}]}"; JSONObject jsonObject = JSONObject.parseObject(str); // 获取到key为shoppingCartItemList的值 String r = jsonObject.getString("shoppingCartItemList"); System.out.println(r); } } package com.cmbc.cms.action.addrebatchno; import java.util.List; import com.alibaba.fastjson.JSONObject; public class Test { public int getId() { return id; } public void setId(int id) { this.id = id; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } private int id; private int num; public static void main(String[] args) { // TODO Auto-generated method stub String str = "{\"id\":\"75\",\"shoppingCartItemList\":[{\"id\":\"407\",\"num\":\"10\"}]}"; JSONObject jsonObject = JSONObject.parseObject(str); // 获取到key为shoppingCartItemList的值 String r = jsonObject.getString("shoppingCartItemList"); System.out.println(r); //取外面的id String id = jsonObject.getString("id"); System.out.println(id); //取里面的id List<Test> list = JSONObject.parseArray(r,Test.class); int id2 = list.get(0).getId(); System.out.println(id2); } }