0

目标:将 XML 解析为 JSON:我已导入 json-org.jar 以将 XML 解析为 JSON。使用简单的java项目很好,但在Android期间它给出了错误:

String stringxml="<item><title>Clinton slams Russia, China over Syria</title></item>";
JSONObject xmlJSONObj = XML.toJSONObject(stringxml);
org.json.JSONObject j = org.json.XML.toJSONObject(stringxml);
String json = j.toString();

我在 DDMS 中遇到错误:java.lang.NoSuchMethodError:org.json.XML.toJSONObject at org.json.XML.toJSONObject(XML.java:282)

4

1 回答 1

1

尝试这样的事情

JSONObject jsonObject = null;           
        FileInputStream is = new FileInputStream("XXX.xml");

        StringBuffer buffer = new StringBuffer();
        InputStreamReader isr = new InputStreamReader(is, "UTF8");
        Reader in = new BufferedReader(isr);
        int ch;
        while ((ch = in.read()) > -1) {
             buffer.append((char)ch);
        }
        in.close();

        String xml = buffer.toString();

        //System.out.println("xml = " + xml);


        jsonObject = XML.toJSONObject(xml);
于 2012-08-23T08:27:49.343 回答