基于Rest Assured+Java+Selenium处理无标识JSON数组的技术问询
Hey there! Let's work through your two questions using the exact libraries you're leveraging: Rest Assured, Java, and org.json. Since your API returns multiple unenclosed JSON objects (no outer array wrapper), we'll first tweak the response to fit a valid JSON structure before processing.
1. Count Total Number of JSON Objects in the Response
Your raw response is a comma-separated list of standalone JSON objects, which isn't a valid JSON array. We'll wrap it in square brackets to convert it into a parsable array, then count the elements.
Code Implementation
import io.restassured.RestAssured; import io.restassured.response.Response; import org.json.JSONArray; public class ResponseCounter { public static void main(String[] args) { // Send POST request and extract response Response response = RestAssured.given() // Add your request config here (headers, request body, auth, etc.) .when() .post("your-target-api-endpoint") .then() .extract() .response(); // Get raw response body string String rawResponseBody = response.getBody().asString(); // Convert unenclosed objects to a valid JSON array String formattedResponse = "[" + rawResponseBody + "]"; // Parse and count objects JSONArray jsonArray = new JSONArray(formattedResponse); int totalObjectCount = jsonArray.length(); System.out.println("Total JSON objects in response: " + totalObjectCount); } }
Alternative: Use Rest Assured's JsonPath
If you prefer using Rest Assured's built-in JsonPath instead of org.json:
import io.restassured.path.json.JsonPath; // ... (same request setup as above) String formattedResponse = "[" + rawResponseBody + "]"; JsonPath jsonPath = JsonPath.from(formattedResponse); int totalCount = jsonPath.getList("$").size();
2. Retrieve stateCountyCode for a Specific cmsContract Value
We'll use the same formatted JSON array, then iterate through objects to find the matching cmsContract and extract the corresponding stateCountyCode.
Code Implementation with org.json
import io.restassured.RestAssured; import io.restassured.response.Response; import org.json.JSONArray; import org.json.JSONObject; public class ContractLookup { public static void main(String[] args) { // Send POST request and extract response Response response = RestAssured.given() // Add your request config here .when() .post("your-target-api-endpoint") .then() .extract() .response(); String rawResponseBody = response.getBody().asString(); String formattedResponse = "[" + rawResponseBody + "]"; JSONArray jsonArray = new JSONArray(formattedResponse); String targetContract = "HB2345"; String matchingCountyCode = null; // Iterate through objects to find the match for (int i = 0; i < jsonArray.length(); i++) { JSONObject currentObj = jsonArray.getJSONObject(i); if (targetContract.equals(currentObj.getString("cmsContract"))) { matchingCountyCode = currentObj.getString("stateCountyCode"); break; // Exit loop once match is found } } // Output result if (matchingCountyCode != null) { System.out.println("stateCountyCode for " + targetContract + ": " + matchingCountyCode); } else { System.out.println("No matching cmsContract found: " + targetContract); } } }
Alternative: Use Rest Assured's JsonPath (One-Liner Style)
JsonPath supports filtering with a predicate, which can simplify the lookup:
import io.restassured.path.json.JsonPath; // ... (same request setup as above) String formattedResponse = "[" + rawResponseBody + "]"; JsonPath jsonPath = JsonPath.from(formattedResponse); // Get list of matching county codes (handles multiple matches if any) List<String> countyCodes = jsonPath.getList("$[?(@.cmsContract == 'HB2345')].stateCountyCode"); if (!countyCodes.isEmpty()) { System.out.println("stateCountyCode: " + countyCodes.get(0)); } else { System.out.println("No match found"); }
内容的提问来源于stack exchange,提问作者S K

