java.lang.NoSuchMethodError:org.json.XML.toJSONObject

时间:2012-07-08 18:57:03

标签: xml json org.json

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

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     在org.json.XML.toJSONObject(XML.java:282)

1 个答案:

答案 0 :(得分: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);