嵌套json字符串转java对象

json数据:


GeSHi Error: GeSHi could not find the language json (using path /home/blog/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import net.sf.json.JSONObject;
//获取json数据中data下的TaskInfo全部内容:为一实例对象
@ResponseBody
@RequestMapping(value = "/ceshi", produces = "application/json; charset=utf-8")
public Map<String, Object> ceshi(HttpServletRequest request, HttpServletResponse response,String TaskId) {
    Map<String, Object> TempMap = new HashMap<String, Object>();
    String data = "{"Status":true,"Message":null,"Data":{"IsCompleted":false,"TaskInfo":{"TaskId":"0013c450-c19f-6620-000c-29d65f600018"}}}";
    //字符串转JSONObject对象-获取Data数据
    JSONObject myJsonObject = JSONObject.fromObject(data);
    for(int i=0;i<myJsonObject.size();i++){
        //判断data下是否存在该key
        if(!myJsonObject.has("TaskId")){
            myJsonObject = JSONObject.fromObject(myJsonObject.get("Data").toString());
        }else{
            myJsonObject = JSONObject.fromObject(myJsonObject.get("TaskId").toString());
            break;
        }
    }
    //Task为实体类
    Task task = (Task) JSONObject.toBean(myJsonObject,Task.class);
    return TempMap;
}
ˆ Back To Top