1 json格式字符串转Map
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
import java.util.Map;
import net.sf.json.JSONObject;
public class JsonToMap{
public static void main(String[] args) {
String json="[{\"id\":1,\"serviceId\":\"2\",\"businessTypeId\":3,\"serviceBusinessKey\":\"4\",\"businessAttribution\":\"5\",\"detailPageUrl\":\"6\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613809720000,\"updateTime\":1613809720000},{\"id\":2,\"serviceId\":\"3\",\"businessTypeId\":3,\"serviceBusinessKey\":\"5\",\"businessAttribution\":\"6\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810937000,\"updateTime\":1613810937000},{\"id\":3,\"serviceId\":\"4\",\"businessTypeId\":5,\"serviceBusinessKey\":\"6\",\"businessAttribution\":\"7\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810945000,\"updateTime\":1613810948000}]";
json=json.replace("[", "");
json=json.replace("]", "");
JSONObject jsonObject = JSONObject.fromObject(json);
Map<Object, Object> maps = (Map) jsonObject;
for (Object map : maps.entrySet()){
System.out.println("key:"+((Map.Entry)map).getKey()+" value:" + ((Map.Entry)map).getValue());
}
}
}
2 json字符串转list
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
import java.util.Arrays;
import java.util.List;
import com.google.gson.Gson;
public class JsonToList{
public static void main(String[] args) {
String jsonString="[{\"id\":1,\"serviceId\":\"2\",\"businessTypeId\":3,\"serviceBusinessKey\":\"4\",\"businessAttribution\":\"5\",\"detailPageUrl\":\"6\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613809720000,\"updateTime\":1613809720000},{\"id\":2,\"serviceId\":\"3\",\"businessTypeId\":3,\"serviceBusinessKey\":\"5\",\"businessAttribution\":\"6\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810937000,\"updateTime\":1613810937000},{\"id\":3,\"serviceId\":\"4\",\"businessTypeId\":5,\"serviceBusinessKey\":\"6\",\"businessAttribution\":\"7\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810945000,\"updateTime\":1613810948000}]";
Student[] array = new Gson().fromJson(jsonString,Student[].class);
List<Student> list = Arrays.asList(array);
Student s=new Student();
for(int i=0;i<list.size();i++) {
s=list.get(i);
System.out.println(s.toString());
}
}
}
import java.util.Date;
public class Student {
private Integer id;
private String serviceId;
private Integer businessTypeId;
private String serviceBusinessKey;
private String businessAttribution;
private String detailPageUrl;
private Integer category1Id;
private Integer category2Id;
private Integer selfSupportType;
private String coverImgUrl;
private String title;
private String note;
private String sellPrice;
private String lable;
private String createTime;
private String updateTime;
@Override
public String toString() {
return "Student [id=" + id + ", serviceId=" + serviceId + ", businessTypeId=" + businessTypeId
+ ", serviceBusinessKey=" + serviceBusinessKey + ", businessAttribution=" + businessAttribution
+ ", detailPageUrl=" + detailPageUrl + ", category1Id=" + category1Id + ", category2Id=" + category2Id
+ ", selfSupportType=" + selfSupportType + ", coverImgUrl=" + coverImgUrl + ", title=" + title
+ ", note=" + note + ", sellPrice=" + sellPrice + ", lable=" + lable + ", createTime=" + createTime
+ ", updateTime=" + updateTime + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ ", toString()=" + super.toString() + "]";
}
}