0

Aim: to parse XML to JSON: i have imported json-org.jar to parse XML to JSON. it was fine working with simple java project but during Android its gives 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();

I have got the Error in DDMS: java.lang.NoSuchMethodError:org.json.XML.toJSONObject at org.json.XML.toJSONObject(XML.java:282)

1
  • 2
    This generally means that the JARs you compiled with are different from the JARs you're running with. Commented Jul 8, 2012 at 18:59

1 Answer 1

1

Try something like this

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.