【问题标题】:How to get JSON from an XML response from server?如何从服务器的 XML 响应中获取 JSON?
【发布时间】:2012-01-18 11:54:09
【问题描述】:

我的 android 应用程序发送一个 Web 服务请求,并以 xml 格式从 Web 服务获取响应,其中嵌入了 JSON 格式的数据。我将它保存在一个字符串中。现在我不知道如何从这个字符串中获取 JSON 数据。

    System.setProperty("http.keepAlive", "false");
            // request parameters
            HttpParams params = httpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 15000);
            // set parameter
            HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

            // POST the envelope
            HttpPost httppost = new HttpPost(url);
            // add headers
            httppost.setHeader("SOAPAction", soapAction);
            httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
    //      httppost.setHeader("Content-Length",
    //              String.valueOf(requestEnvelope.length()));
            httppost.setHeader("SOAPAction", "http://tempuri.org/"
                    + methodName);



    //      String responseString = "";
            try {

                // the entity holds the request
                HttpEntity entity = new StringEntity(requestEnvelope);
                httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
            String result = EntityUtils.toString(response.getEntity());

这是来自服务器的响应,我在结果字符串中得到。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ValidatePassCodeResponse xmlns="http://tempuri.org/">
<ValidatePassCodeResult>
[{"ID":1929,"Headline":"Test News","Detail":"","SubmitDate":"1/17/2012 12:08:04 PM"}]
</ValidatePassCodeResult>
</ValidatePassCodeResponse>
</soap:Body>
</soap:Envelope>

提前致谢。

【问题讨论】:

    标签: android xml json


    【解决方案1】:

    这就是我删除xml字符串的标签并从中获取json字符串的方法,然后我将字符串解析为json数组并获得结果。

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser xpp = factory.newPullParser();
    
                xpp.setInput(new StringReader(result));
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_DOCUMENT) {
                        System.out.println("Start document");
                    } else if (eventType == XmlPullParser.START_TAG) {
                        System.out.println("Start tag " + xpp.getName());
                    } else if (eventType == XmlPullParser.END_TAG) {
                        System.out.println("End tag " + xpp.getName());
                    } else if (eventType == XmlPullParser.TEXT) {
                        responseString = xpp.getText();
                        System.out.println("Text " + xpp.getText());
                    }
                    eventType = xpp.next();
                }
                System.out.println("End document");
    

    【讨论】:

      【解决方案2】:

      请查看我的问题并将您的响应字符串放入 json 查看器中。

      How to use Json Parsing?

      【讨论】:

        【解决方案3】:

        首先使用 SAXParser 检索 JSON。
        看到这个sample
        然后解析 JSON。看到这个question

        【讨论】:

          【解决方案4】:

          我使用XMLPullParser 得到了解决方案。 不过感谢您的帮助。

          【讨论】:

          • ... 答案是什么?如果您写下您发现的内容,此问题的下一位读者可能会受益。
          猜你喜欢
          • 2019-02-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-09
          • 2018-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多