一 操作说明
1.1 说明
本案例操作说明,json,bean,相互之间转换成JSONObject,jsonarray。
pom依赖的配置
<!-- ailibaba json转换 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
1.2 实施操作
1.2.1 定义初始javabean
1.Apple类
public class Apple {
private String id;
private String pName;
private String price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
2.city类
public class City {
private int id;
private String cName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
}
3.province类
public class Province {
private int id;
private String pName;
private List<City> cityList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public List<City> getCityList() {
return cityList;
}
public void setCityList(List<City> cityList) {
this.cityList = cityList;
}
}
1.2.2 bean和jsonobject,json串
public static void jsonObjectInfo(){
Apple apple=new Apple();
apple.setId("24444");
apple.setpName("pingguo234");
apple.setPrice("345.69");
Gson gson =new Gson();
String gjson= gson.toJson(apple);
System.out.println("gson:"+gjson);
//1. 实体变json串
String tojson= JSONObject.toJSON(apple)+"";
System.out.println("tojson:"+tojson);
//2. json串变JSONObject
JSONObject appleJsonObject= JSONObject.parseObject(gjson);
String id=appleJsonObject.get("id")+"";
String name=appleJsonObject.getString("pName");
System.out.println("id:"+id+" name: "+name);
}
调用结果:
1.2.3 bean和jsonobjectArray
1.代码
public static void jsonArrayInfo(){
Province provice=new Province();
provice.setId(11);
provice.setpName("北京");
List<City> cityList1=new ArrayList<>();
City city=new City();
city.setId(11);
city.setcName("朝阳");
cityList1.add(city);
City city2=new City();
city2.setId(12);
city2.setcName("海淀");
cityList1.add(city2);
City city3=new City();
city3.setId(13);
city3.setcName("西城");
cityList1.add(city3);
provice.setCityList(cityList1);
//1.实体转json
String str = JSONObject.toJSON(provice).toString();
System.out.println("str: "+str);
//2.json 转 json array
JSONObject appObj= JSONObject.parseObject(str);
String name=appObj.getString("pName");
System.out.println("pName:"+name);
//3.获取JSONObject对象中的JSONArray数组
JSONArray arrayList= appObj.getJSONArray("cityList");
for(Object obj:arrayList){
JSONObject jsonObject=(JSONObject)obj;
String cname=jsonObject.get("cName")+"";
System.out.println("cname:"+cname);
}
}
2.结果
1.3 list转json,转jsonArray
public static void toArrayList(){
List<City> cityList1=new ArrayList<>();
City city=new City();
city.setId(11);
city.setcName("朝阳");
cityList1.add(city);
City city2=new City();
city2.setId(12);
city2.setcName("海淀");
cityList1.add(city2);
City city3=new City();
city3.setId(13);
city3.setcName("西城");
cityList1.add(city3);
//1.实体转json
String str = JSONObject.toJSON(cityList1).toString();
System.out.println("str: "+str);
JSONArray jsonArray= JSONArray.parseArray(str);
for(int k=0;k<jsonArray.size();k++){
JSONObject c=(JSONObject) jsonArray.get(k);
String nt= c.get("cName")+"";
System.out.println("name:"+nt);
String cstr = JSONObject.toJSON(c).toString();
System.out.println("cstr:"+cstr);
}
}
截图