java 字符串转json,json转实体对象、json字符串转换成List、List转String、以及List排序等等...

时间:2023-12-09 23:31:31
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST)
@ResponseBody
public void updateInvestorApplyAccountNo(HttpServletRequest request,
HttpServletResponse response,
@RequestBody String requestBody) {
int num = 0;
String result = "";
//下面是把拿到的json字符串转成 json对象
JSONObject jsStr = JSONObject.parseObject(requestBody); //将字符串{“id”:1}
//int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值 /**
* json对象转换成java对象
*/
InvestorApplyModel stud = (InvestorApplyModel) JSONObject.toJavaObject(jsStr,InvestorApplyModel.class);
/**
* json 字符串转换成java对象
*/ gascontent model=new gascontent();
model = JSON.parseObject(JSON.parse(json.getString("content")).toString(),gascontent.class);

/**
* json字符串转换成List
*/
String data=json.getJSONObject("content").getString("data");

List<Data>  listdata=JSONObject.parseArray(data, Data.class);
/**
* List转换成String
*/
String json2=JSON.toJSONString(listdata);
} 

List 实体类对象排序,首先实体类要实现 Comparable  ,然后在实体类里面重写 compareTo 方法,此方法里面写你要设置的排序属性。

public class Data implements Comparable<Data>{

    private String area;
private String fwlsmc;
private String address;
private String areaname;
private int distance;
private String exhaust;
private String discount;
private String lon;
private String brandname;
private String type;
private Price price;
private String name;
private String id;
private String position;
private String lat;
public void setArea(String area) {
this.area = area;
}
public String getArea() {
return area;
} public void setFwlsmc(String fwlsmc) {
this.fwlsmc = fwlsmc;
}
public String getFwlsmc() {
return fwlsmc;
} public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
} public void setAreaname(String areaname) {
this.areaname = areaname;
}
public String getAreaname() {
return areaname;
} public void setDistance(int distance) {
this.distance = distance;
}
public int getDistance() {
return distance;
} public void setExhaust(String exhaust) {
this.exhaust = exhaust;
}
public String getExhaust() {
return exhaust;
} public void setDiscount(String discount) {
this.discount = discount;
}
public String getDiscount() {
return discount;
} public void setLon(String lon) {
this.lon = lon;
}
public String getLon() {
return lon;
} public void setBrandname(String brandname) {
this.brandname = brandname;
}
public String getBrandname() {
return brandname;
} public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
} public void setPrice(Price price) {
this.price = price;
}
public Price getPrice() {
return price;
} public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
} public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
} public void setPosition(String position) {
this.position = position;
}
public String getPosition() {
return position;
} public void setLat(String lat) {
this.lat = lat;
}
public String getLat() {
return lat;
} // 重写 比较方法 本次例子定义为按距离比较
@Override
public int compareTo(Data stu) {
if (this.distance > stu.getDistance()) {
return 1;
} else {
return -1;
}
} }

排序调用示例:

   List<Data> listdata=new ArrayList<>();

   Collections.sort(listdata); //只有一个参数参数为List