无法导入Jsonbject,当我尝试导入时出现错误。

huangapple 未分类评论54阅读模式
英文:

Not able to import Jsonbject and when I do I get an error

问题

不确定我在这里做错了什么,请帮忙。即使在导入时我也会得到“JSONObject cannot be resolved to a type”的错误。错误在这里:“final JSONObject json = new JSONObject();”以及在这里无法解析对象:“final String 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);
英文:

not sure what I'm doing wrong here, please. even when I import I get JSONObject cannot be resolved to a type. error here "final JSONObject json = new JSONObject();" and object cannot be resolved here "final String 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);

答案1

得分: 0

问题JSONObject cannot be resolved to a type可能是因为您没有正确导入库。类JSONObject似乎属于库org.json。因此,如果您使用Maven或Gradle,请按照链接中的描述导入它。

对于这行代码的问题:

final String jsonString = obj.toString();

是因为变量obj在您的代码中未定义。也许您的意思是:

final String jsonString = json.toString();
英文:

The issue JSONObject cannot be resolved to a type is probably because you haven't import the library correctly. The class JSONObject seems to belong to the library org.json. So if you use Maven or Gradle, import it as described in the link.

The issue with this line:

final String jsonString = obj.toString();

Is because the variable obj is not defined in your code. Maybe you mean:

final String jsonString = json.toString();

huangapple
  • 本文由 发表于 2020年7月27日 10:50:30
  • 转载请务必保留本文链接:https://java.coder-hub.com/63108023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定