我正在尝试使用Java语言和Netbeans(作为IDE)构建天气应用程序。代码如下:
URL link = new URL("http://api.dataweave.in/v1/weather/findByCity/?api_key=a93abfaea2a98410be0df095f72df25d7a5d5eb5&city=Shimla&date=20130501");
URLConnection uc1 = link.openConnection();
BufferedReader in1 = new BufferedReader(new InputStreamReader(uc1.getInputStream()));
StringBuilder weather = new StringBuilder();
weather.append(in1.readLine() + "\n");
String line="0";
while ((line = in1.readLine()) != null) {
weather.append(line + "\n");
}
String result=weather.toString();
JSONParser parser=new JSONParser();
JSONObject obj = (JSONObject)parser.parse(result);
JSONArray jarr = (JSONArray) obj.get("data");
System.out.println(((JSONObject) jarr.get(0)).get("city"));
System.out.println(((JSONObject) jarr.get(0)).get("country"));
System.out.println(((JSONObject) jarr.get(0)).get("min_today"));
System.out.println(((JSONObject) jarr.get(0)).get("max_today"));
System.out.println(((JSONObject) jarr.get(0)).get("desc_today"));
System.out.println(((JSONObject) jarr.get(0)).get("date"));
在线
JSONParser parser=new JSONParser();
JSONObject obj = (JSONObject)parser.parse(result);
我收到此错误:
constructor JSONParser in class JSONParser cannot be applied to given types
required: Source, Error Manager
found: No arguments
reason: actual and formal arguments list differs in length
我已经拥有" json-simple" jar文件作为库。现在该怎么办?
答案 0 :(得分:7)
检查您的import语句。您将从jdk.nashorn导入它。 您尝试尝试的代码来自org.json.simple.parser.JSONParser
答案 1 :(得分:0)
删除 import jdk.nashorn.internal.parser.jsonparser;
添加 import org.json.simple.parser.JSONParser;