使用FastJson从json串中根据key获取value

时间:2022-04-17 20:22:44
 1 import com.alibaba.fastjson.JSONObject;
 2 
 3 /**
 4  * Created by SYJ on 2017/9/13.
 5  */
 6 public class MainTest {
 7     public static void main(String[] args) {
 8         String json = "{\"code\":0,\"message\":\"没有查询到数据\",\"data\":null,\"taskId\":\"0-bb03193f-ed1f-4d32-8351-6538d329b26e\",\"time\":\"2017-09-2210:11:30\"}";
 9         JSONObject obj = JSONObject.parseObject(json);
10         Integer code = obj.getInteger("code");
11         String message = obj.getString("message");
12         String taskId = obj.getString("taskId");
13 
14         System.out.println("code:" + code + ", message:" + message + ", taskId:" + taskId);
15     }
16 }