1. 使用google依赖
<dependency> <groupId>com.vaadin.external.google</groupId> <artifactId>android-json</artifactId> <version>0.0.20131108.vaadin1</version> <scope>test</scope> </dependency>
import org.json.JSONException;
import org.json.JSONObject;
@Test
public void testJsonToObject() throws JSONException {
//把json字符串转为json对象
String jsonString = "{\"notBefore\":\"2019.11.09 15:09:00\",\"notAfter\":\"2022.07.24 15:09:00\"}";
JSONObject jsonObj = new JSONObject(jsonString);
System.out.println(jsonObj.get("notBefore"));
}
2. 使用alibaba依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
import com.alibaba.fastjson.JSONObject;
@Test
public void testJsonToObject2(){
String ss = "{\"notBefore\":\"2019.11.09 15:09:00\",\"notAfter\":\"2022.07.24 15:09:00\"}";
JSONObject json = JSONObject.parseObject(ss);
System.out.println(json.getString("notBefore"));
}