2016-11-17 56 views
-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; 
    } 

    }} 
+2

請向我們展示進口報表,因爲它們似乎很重要。 – Seelenvirtuose

+2

檢查您的進口。您似乎一起使用了兩個JSON解析庫,並導入了錯誤的「JSONObject」。 – RealSkeptic

+0

我已經解決了,謝謝,但我得到另一個錯誤 – codehacker

回答

-1

在你的代碼進口上述這一切您遇到import org.json.JSONObject,改變這種以import org.json.simple.JSONObject應該幫助。

很可能你會有其他進口org.json.x你必須更改爲org.json.simple.x