简体   繁体   中英

Rest-assured. Is it possible to extract JSONObject/JSONArray from response json?

I'm Using RestAssured, and i'm getting the response like this-

{
    "Data": {
        "Sub": {
            "SubDetails": [
                {
                    "OrgId": 5,
                    "SubId": 1,
                    "SubName": "Mathematics"
                }
            ]
        }
    },
    "RawData": {
        "Url": "http://localhost:11111/cases/case-15",
        "Type": "Rest",
        "Request": {
            "Details": {
                "OrganizationId": 5,
                "Student": {
                    "Age": 30,
                    "Religion": "Hindu",
                    "StudentId": 10
                }
            }
        },
        "Response": {
            "SmartReturnObject": {
                "Subject": [
                    {
                        "SubjectId": 1,
                        "SubjectName": "Mathematics"
                    }
                ],
                "OrganizationId": 5
            }
        },
        "IsApiError": false
    },
    "SessionId": "5q0",
    "RequestUniqueId": "4543534",
    "StatusCode": "4540000",
    "StatusMessage": "Success",
    "DataSource": "DD"
}  

Now i need to extract the Data object separate and RawData separate but i'm failed.
I tried with JSONPath also but not getting it in JSON format.
I tried

JsonPath body= response.jsonPath();
          Object value=body.get("Data");
          System.out.println("Value is-"+value);  

and getting it like this - {Sub={SubDetails=[{OrgId=5, SubId=1, SubName=Mathematics}]}}
I want the response as instanceof JSONObject or JSONArray type.

pom.xml

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>

Try below -

Gson gson = new Gson();
try {
     JSONObject obj = new JSONObject(gson.toJson(givenString));
}catch (Exception e){
     System.out.println(e.toString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM