我使用以下代码: -
SELECT eid, cruising_range FROM aircraft JOIN certified USING (aid) JOIN (
SELECT eid FROM certified GROUP BY eid HAVING COUNT(*) > 3
) t USING (eid)
我也在下面的行中收到错误: -
import org.json.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.Assert;
import org.junit.Test;
import network.Authorization;
import network.ContentType;
import network.HTTPHelper;
import network.HTTPRequest;
import network.HTTPResponse;
import static org.junit.Assert.*;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public void testSendPOSTRequest() {
try {
HTTPRequest request = new HTTPRequest();
request.url = "https://myURL/api/products";
request.contentType = ContentType.JSON;
Map<String, String> authKeyValue = new HashMap<>();
authKeyValue.put(Authorization.Type.toString(), "Token token=zkz,email=test7@gmail.com");
request.setAuthorization(authKeyValue);
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("./src//productApi"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("Name");
String author = (String) jsonObject.get("Author");
JSONArray companyList = (JSONArray) jsonObject.get("Company List");
System.out.println("Name: " + name);
System.out.println("Author: " + author);
System.out.println("\nCompany List:");
Iterator<String> iterator = companyList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (Exception e) {
e.printStackTrace();
}
HTTPHelper helper = new HTTPHelper();
HTTPResponse response = helper.sendPOSTRequest(request);
System.out.println("POST Success");
System.out.println("Response code: " +response.statusCode.toString());
System.out.println("Payload: " +response.payload);
assertTrue(true);
} catch (Exception e) {
System.out.println(""+e.getMessage());
assertTrue(false);
} finally {
System.out.println("Exist Run");
}
Eclipse将提示/错误显示为: -
对于JSONArray类型
,未定义方法iterator()添加到演员阵容
任何人都可以给我一个上述问题或任何替代方法的解决方案,以便我可以从文件中读取JSON对象并将其直接传递给有效负载作为请求
答案 0 :(得分:2)
假设您正在使用org.json.JSONArray
个对象。
关于&#34; iterator()未定义&#34;你遇到的问题。您是因为iterator()
类的JSONArray
未定义而导致的。
如果您确实要打印JSONArray
对象,可以使用以下内容:
System.out.println(companyList);
而不是
Iterator<String> iterator = companyList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
这应该删除错误:
对于JSONArray类型
,未定义方法iterator()
这是因为以下toString()定义。据我所知,这会产生一个有效的JSON字符串。您应该只能在响应数据中使用companyList.toString()
。事实上,根据this page,以下是&#34;正确的&#34;序列化JSONObject的方法:
JSONObject object = ...
String json = object.toString();
您还可以通过执行以下操作遍历JSONArray中的对象:
for(int i = 0; i < companyList.length(); i++){
Object obj = companyList.get(i); //Then use obj for something.
}
如果您知道数据类型,那么您也可以使用任何other get alternatives。
答案 1 :(得分:1)
使用Jackson ObjectMapper,您可以从如下文件中读取:
final InputStream is =
Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
String data=null;
try {
data = new ObjectMapper().readValue(is, String.class);
} catch (final Exception e) {
//Handle errors
}
然后,将此字符串数据添加到您的http请求中是微不足道的。
答案 2 :(得分:-1)
Jackson的ObjectMapper可用于JSON序列化/反序列化。
示例:
ObjectMapper objectMapper = new ObjectMapper();
String carJson =
"{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
try {
Car car = objectMapper.readValue(carJson, Car.class);
System.out.println("car.brand = " + car.brand);
System.out.println("car.doors = " + car.doors);
} catch (IOException e) {
e.printStackTrace();
}