I am using the below code:-
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 protected]");
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");
}
I am also getting error in below line:-
Iterator<String> iterator = companyList.iterator();
Eclipse showing as tips/error as :-
The method iterator() is undefined for the type JSONArray
Add to cast to
Can anyone give me a solution of above problem or any alternative way so I can read a JSON object from file and pass it directly to payload as request