[Java] get json java in a field

Import com.alibaba.fastjson.JSONObject;
 public  class JsonTest {
     public  static  void main (String [] args) {
         // JSON string (with their subject) 
        String STR = "{" ID ":" 75 "," shoppingCartItemList ": [{" ID ":" 407 "," NUM ":" 10 "}]}" ; 
        the jSONObject jsonObject = JSONObject.parseObject (STR);
         // Get the value of the key is shoppingCartItemList 
        String r = jsonObject.getString ( "shoppingCartItemList" ); 
        System.out.println (R & lt); 
    } 
} 
Package com.cmbc.cms.action.addrebatchno;
 
import java.util.List; 
 
importcom.alibaba.fastjson.JSONObject; 
 
public  class Test { 
 
public  int getId () { 
 return id; 
} 
 
Public  void setID ( int id) { 
 this hu = id; 
} 
 
Public  int getner () { 
 return num; 
} 
 
Public  void setnis ( 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); 
 
} 
 
}

 

Guess you like

Origin www.cnblogs.com/jxd283465/p/11612168.html