-1
這是我的代碼,用於從json數據中獲取與它關聯的屬性名稱和值。該代碼在沒有錯誤的情況下執行,但我得到的結果爲空。使用Java來驗證JSON數據
import java.io.*;
import java.net.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class A18 {
public static void main(String[] args) throws ParseException{
String[] out2;
String out,out1= null;
try{
URL a=new URL("URL");
HttpURLConnection b=(HttpURLConnection) a.openConnection();
b.setRequestMethod("GET");
b.setRequestProperty("Accept", "application/json");
BufferedReader c=new BufferedReader(new InputStreamReader(b.getInputStream()));
StringBuilder sb=new StringBuilder();
while((out=c.readLine())!=null)
{
sb.append(out);
out1=sb.toString();
}
c.close();
b.disconnect();
JSONObject obj = new JSONObject();
String id = obj.toJSONString("collection");
String error = obj.toJSONString("links");
}
catch (Exception e)
{
e.printStackTrace();
return;
}
}}
請向我們展示進口報表,因爲它們似乎很重要。 – Seelenvirtuose
檢查您的進口。您似乎一起使用了兩個JSON解析庫,並導入了錯誤的「JSONObject」。 – RealSkeptic
我已經解決了,謝謝,但我得到另一個錯誤 – codehacker