我从API调用获得以下响应:
[{"id":63,"name":"Apple Inc.","ticker":"AAPL","website":"www.apple.com","street1":null,"street2":null,"country":null,"postal_code":null,"city":null,"state":null,"type_value":"PUBLIC","person":[{"id":6854208},{"id":6854192},{"id":7795},{"id":6837866},{"id":6854188},{"id":6840774},{"id":6838278},{"id":7637},{"id":6839671},{"id":6837862},{"id":6840759},{"id":6840766},{"id":6838242},{"id":6840830},{"id":6840838},{"id":7875},{"id":3038662},{"id":865765},{"id":6839669},{"id":6837834},{"id":6839685},{"id":6839931},{"id":6840777},{"id":6838232},{"id":6838260},{"id":1859904},{"id":6854204},{"id":6838238},{"id":6839751}],"type":3001,"revenue":null,"industry":[{"industry":"Computer Hardware","industry_id":5009},{"industry":"Electronics","industry_id":5016},{"industry":"Technology - All","industry_id":5044}],"description":null}]
如何从响应中获取名称的值?
这就是我现在所拥有的:
System.err.println("fullBspApiUrl"+fullBspApiUrl);
Response resp = get(fullBspApiUrl);
System.err.println("This is response"+resp);
String bJson = resp.asString();
System.err.println("This is response after string conversion"+bJson);
JsonPath jsonpath = new JsonPath(bJson);
System.err.println("Instantiate JsonPath "+jsonpath);
//String bspOrgName = jsonpath.getString("organizationName[0]");
String bspOrgName = jsonpath.getString("organizationName[0]");
System.err.println("This is response after JsonPath string conversion "+bspOrgName);
assertEquals(resp.getStatusCode(),200);
assertEquals(bspOrgName,"Apple Inc");
它返回
Null
而不是Apple
。 最佳答案
get(fullBspApiUrl).then().statusCode(200).
and().assertThat().body(name[0], equalTo("Apple Inc"))
关于java - Jayway Rest向Json保证了字符串转换的路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32575967/