2016-11-17 33 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