请不确定我在做什么错。即使我导入时,我也无法将JSONObject解析为一种类型。错误“最终JSONObject json =新的JSONObject();”和对象无法在此处解析“最终字符串jsonString = obj.toString();”
package maven;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
// import org.apache.http.client.HttpClient;
// import org.apache.http.client.methods.HttpPost;
// import org.apache.http.entity.StringEntity;
// import org.apache.http.HttpResponse;
// import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.simple.JSONObject;
final JSONObject json = new JSONObject();
json.put("id", "uuuu@mail.edu");
json.put("name", "Jon doe");
json.put("average", calculateClassAverage(students));
json.put("studentIds", femaleIDs.toArray());
final URL url = new URL ("http:weblink");
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
final String jsonString = obj.toString();
System.out.println("JSON Request String: ");
System.out.println(jsonString);
答案 0 :(得分:0)
问题JSONObject cannot be resolved to a type
可能是因为您没有正确导入库。类JSONObject
似乎属于库org.json。因此,如果您使用Maven或Gradle,请按照链接中的说明进行导入。
此行的问题:
final String jsonString = obj.toString();
是因为在您的代码中未定义变量obj
。也许你是说:
final String jsonString = json.toString();