首页 > 解决方案 > 解析 JSON 响应

问题描述

我正在尝试使用下面的 json 响应并在 java中获取“ message”和“ ”数据WORKORDERID

{
    "operation": {
        "result": {
            "message": " successfully.",
            "status": "Success"
        },
        "Details": {
            "SUBJECT": "qqq",
            "WORKORDERID": "800841"
        }
    }
}

下面是我的代码

JSONObject inputs = new JSONObject(jsonResponse);
JSONObject jsonobject = (JSONObject) inputs.get("operation");
String s = jsonobject.getString("message");
system.out.println("s");

标签: javajsonrestful-urlphrase

解决方案


您的对象嵌套了 2 次,因此您应该这样做:

JSONObject inputs = new JSONObject(jsonResponse);

JSONObject operation= (JSONObject) inputs.get("operation");
JSONObject result= (JSONObject) operation.get("result");
JSONObject details= (JSONObject) operation.get("Details");
String message = result.getString("message");
String workerId = details.getString("WORKORDERID");

推荐阅读