如何解析XML包装JSON [英] How to parse the xml wrapping json

查看:269
本文介绍了如何解析XML包装JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何一个可以告诉我如何解析以下

Can Any one tell me How to parse the following

    <string xmlns="http://tempuri.org/">
[{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_3@2x.png","ImageID":"3"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_4@2x.png","ImageID":"4"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_5@2x.png","ImageID":"5"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_6@2x.png","ImageID":"6"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_7@2x.png","ImageID":"7"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_8@2x.png","ImageID":"8"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_9@2x.png","ImageID":"9"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_10@2x.png","ImageID":"10"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_11@2x.png","ImageID":"11"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_12@2x.png","ImageID":"12"}]
</string>

我打我的一些paramaeters用下面的方法的帮助URL,是不是?

I am hitting my url with some paramaeters with the help of following method , is it right ?

 public String getJSON(String url, int timeout) {
                HttpURLConnection c = null;
                try {
                    URL u = new URL(url);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    c.setRequestProperty("Content-length", "0");
                    c.setUseCaches(false);
                    c.setAllowUserInteraction(false);
                    c.setConnectTimeout(timeout);
                    c.setReadTimeout(timeout);
                    c.setRequestProperty("Content-Type", "application/json");
                    c.connect();
                    int status = c.getResponseCode();

                    switch (status) {
                        case 200:
                        case 201:
                            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                            StringBuilder sb = new StringBuilder();
                            String line;
                            while ((line = br.readLine()) != null) {
                                sb.append(line+"\n");
                            }
                            br.close();
                            return sb.toString();
                    }

                } catch (MalformedURLException ex) {
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (c != null) {
                        try {
                            c.disconnect();
                        } catch (Exception ex) {
                            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                return null;
            }

是正确的做,如何解析响应,请回答我带源$ C ​​$ C。

is Right to do and How to parse the response please answer me with source code.

推荐答案

感谢所有的答复,让我明白做什么步骤,我需要做到这一点。嗯,我询问如何删除XML标记时解析XML清楚这是很明显的,解析XML是比较JSON稍微棘手,是从我学会了如何分析它,但交代如何解析这个XML响应教程它没有命名空间,也没有方法名等。

Thanks for all for the replies and make me understand what steps do I need to do this. Well I was asking about how to remove the xml tags when parsing xml well it was quite obvious that parsing xml is little more tricky as compare to json and yes from the tutorial I learnt how to parse it but was answerable how to parse this xml response which has no name space and no method name etc

所以我张贴这是一个答案,如果在相同的情况下,某一个陷阱(特别是网页设计师/服务器端程序员把它放在你的时候:()

So I am posting this as an answer if some one trap in the same case (specially when web designers/server side programmers putting it on you :( . )

我用下面的类与出名字空间解析XML和R返回字符串我。在code是这样如下

I have used the following class to parse the xml with out name space and r returned me string. The code goes like as following

public class StringXmlParser {
// your xml doesn't have any name spacing so make it null.
private static final String ns = null;

public String parse(InputStream in) throws XmlPullParserException, IOException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in,null);
        parser.nextTag();
        return readString(parser);
    } finally {
        in.close();
    }
}

private String readString(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "string");
    String jsonString = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "string");
    return jsonString;
}

private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

}

这解决了我的情况。我希望,如果有人想同样的解决方案,然后它可以帮助他。刚刚上传的帮助初学者纯正的唯一目的答案。

This has solved my case. I hope if some one want the same solution then it may help him out. Just uploading the answer for the sole purpose of helping pure beginners.

这篇关于如何解析XML包装JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆