解析json时,prolog中不允许使用内容

时间:2014-04-05 18:32:33

标签: 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"


        // 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.

感谢任何帮助。

0 个答案:

没有答案