Java 解析json时,prolog中不允许包含内容

Java 解析json时,prolog中不允许包含内容,java,json,url,Java,Json,Url,我尝试使用以下代码解析json文件 public class JSONParsing { private static final String XMLOutput = "C:/Users/Content/Downloads/outputxml.txt"; public static void main(String[] args) throws Exception { try{ String URLfile = "xxxx" //

我尝试使用以下代码解析json文件

public class JSONParsing {

private static final String XMLOutput = "C:/Users/Content/Downloads/outputxml.txt";

public static void main(String[] args) throws Exception {

        try{


        String URLfile = "xxxx"


        // Step 1> 
        // Connecting to the Website to get the JSON file
        URL url = new URL(URLfile);
        URLConnection conn = url.openConnection();

        // Step 2> to get the information as an 'input stream'
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(conn.getInputStream());

        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer xform = tfactory.newTransformer();

        File myOutput = new File(XMLOutput);
        xform.transform(new DOMSource(doc), new StreamResult(myOutput));
    InputStream is = 
            JSONParsing.class.getResourceAsStream( "C:/Users/Content/Downloads/outputxml.txt");
    String jsonTxt = IOUtils.toString( is );

    JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );        
    int listingCount = json.getInt( "listingCount" );
    double totalBudget = json.getDouble( "totalBudget" );
    double cpc = json.getDouble("cpc");


    System.out.println( "Listing Count: " + listingCount );
    System.out.println( "Total Budget: " + totalBudget );
    System.out.println( "CPC :"+cpc);

}catch(Exception e){
    System.out.println("There is an error :"+e);
}

}
}

我的输入文件的格式如下-

{"listingCount":10,"totalBudget":25000.00,"cpc":0.54,"ctr":null}
错误

 Error : [Fatal Error] :1:1: Content is not allowed in prolog.
 There is an error :org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

非常感谢您的帮助。

您的输入不是有效的xml文档,它是jsonRead。您为什么要尝试使用xml解析器解析JSON?@IanRoberts:抱歉,这是我第一次尝试解析JSON,您是在询问JSON解析器:JSONParser还是在询问DOM builder。请帮助我理解它。如果URLFile真的是JSON,那么就放弃整个步骤2部分,只说String jsonText=IOUtils.toStringconn.getInputStream,UTF-8;