奇怪的SOAP响应,是JSON?如何分析呢? [英] Weird SOAP response, is it JSON? How to parse it?

查看:134
本文介绍了奇怪的SOAP响应,是JSON?如何分析呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ksoap2 消耗 SOAP 根据 web服务响应的格式的我得到是这样的:

  anyType的{
    键1 =值;
    键2 =值;

    KEY3 = {anyType的

        KEY4 =值4;
        KEY5 =值5;
        KEY6 = {anyType的
                KEY7 = {anyType的
                    key8 = value8;
                };
            };

    };

    key9 = value9;
}
 

这是 JSON对象(如果我们假设这是 JSON )开始, anyType的{和结束与} 键和值由 = ,和; 的字段分隔符/语句结束/不管

我试图验证使用在线验证的响应字符串,但失败了。这指出,这不是一个有效的 JSON对象

A 类似的例子可以发现<一href="http://stackoverflow.com/questions/1270191/how-to-parse-this-web-service-response-in-android">in这个问题。但在接受的答案并没有为我工作,因为,第一响应字符串不以 { anyType的{,如果我把 anyType的{如果的条件,但仍然引发除了接下来遇到,当时间 anyType的{嵌套JSON对象

第二个答案似乎工作在一定程度上,但问题是,我的整个响应字符串看起来是作为一个单独的属性(因为propertyCount为1),所以当我打印出来的名称或属性值,整个响应字符串被打印。

我用Google搜索了很多,并试图一切我能找到。所以,我想我会分析它自己。

我的问题是什么是要分析这种反应的最好方法。

我应该尝试使用解析它正则表达式的我应该将响应字符串到 JSON格式通过替换的 anyType的{所有出现 { = ; 等等,等等,然后通过这样的字符串转换为JSONObject的:

 的JSONObject =新的JSONObject(responseString);
 

,然后通过类似提取键和值:

 迭代器ITR = jsonObject.keys();

        而(itr.hasNext()){
            字符串值= NULL;

            字符串键=(字符串)itr.next();
            尝试 {
                值= jsonObject.getString(密钥);
            }赶上(JSONException E){
                e.printStackTrace();
            }

            Log.i(TAG,键+:+值);

            // ......
        }
 


 进口java.util.Iterator的;

进口org.json.JSONException;
进口org.json.JSONObject;

公共类JSONPracticeOne {

    私有静态的JSONObject的JSONObject;

    私有静态字符串键;
    私有静态字符串值;

    公共静态无效的主要(字串[] args){

        字符串responseString = anyType{key1=value1;key2=value2;key3=anyType{key4=value4;key5=value5;key6=anyType{key7=anyType{key8=value8};};};key9=value9;}";

        responseString = responseString.replaceAll(anyType的\\ Q \\ {E,{);

        responseString = responseString.replaceAll(\\ Q = \\ E,:);

        responseString = responseString.replaceAll(;,,);

        responseString = responseString.replaceAll(\\ Q} \\ E,});

        //System.out.println(responseString);

        //System.out.println();

        responseString = responseString.replaceAll((:\\ {), - ); //替换:{通过 - 
        responseString = responseString.replaceAll([:],\:\); //替换:经:
        responseString = responseString.replaceAll( - ,\:{\); //替换 - 回:{

        //System.out.println(responseString);

        //System.out.println();

        responseString = responseString.replaceAll([,],\);

        //System.out.println(responseString);

        //System.out.println();

        // string字符串= responseString.charAt(1)+;的System.out.println(扯扯+字符串);

        responseString = responseString.replaceFirst([\\ {],{\);

        //System.out.println(responseString);

        //System.out.println();

        // responseString = responseString.replaceAll(([^ \\}],),\,); //截断

        responseString = responseString.replaceAll((\\},), - ); //取代},通过 - 

        responseString = responseString.replaceAll(,,\,); //替换,由,

        responseString = responseString.replaceAll( - ,}); //更换 - 回},

        //System.out.println(responseString);

        //System.out.println();

        responseString = responseString.replaceAll((小于?![\\}])\\},\});

        的System.out.println(responseString);

        System.out.println("**********************************************************************************************\n\n");}}
 

输出: -

  {
    KEY1:值1,
    KEY2:值2,
    KEY3:{
        KEY5:值5,
        KEY6:{
            KEY7:{
                key8:value8
            }
        },
        KEY4:值4
    },
    key9:value9
}
 

解决方案

响应不是JSON,它是类JSON对象,你可以使用kso​​ap2的能力对其进行解析。

在<一个href="https://github.com/mosabua/ksoap2-android/blob/master/ksoap2-base/src/main/java/org/ksoap2/serialization/SoapObject.java">SoapObject.java,也有一些方法,如如下:

 公开对象的getProperty(INT指数){
        对象道具= properties.elementAt(指数);
        如果(托的instanceof的PropertyInfo){
            返程((的PropertyInfo)道具).getValue();
        } 其他 {
            返回((SoapObject)丙);
        }
    }
 

  / **
*获取属性的toString值。
*
* @参数指标
* @返回
* /
    公共字符串getPropertyAsString(INT指数){
        的PropertyInfo的PropertyInfo =(的PropertyInfo)properties.elementAt(指数);
        返回propertyInfo.getValue()的toString()。
    }
 

  / **
*获取属性与给定的名称
*
* @throws了java.lang.RuntimeException
如果属性不存在*
* /
    公共对象的getProperty(字符串名称){
        整数指数= propertyIndex(名称);
        如果(指数!= NULL){
            返回的getProperty(index.intValue());
        } 其他 {
            抛出新的RuntimeException(非法财物+姓名);
        }
    }
 

  / **
*获取属性的toString值。
*
* @参数名称
* @返回
* /

    公共字符串getPropertyAsString(字符串名称){
        整数指数= propertyIndex(名称);
        如果(指数!= NULL){
            返回的getProperty(index.intValue())的toString()。
        } 其他 {
            抛出新的RuntimeException(非法财物+姓名);
        }
    }
 

等。

和您可以尝试解析你的对象是这样的:

 尝试{
            androidHttpTransport.call(SOAP_ACTION,包);
            SoapObject响应=(SoapObject)envelope.getResponse();

            如果(response.toString()。等于(anyType的{})||响应== NULL){
                返回;
            } 其他 {
                字符串值1 = response.getPropertyAsString(键1);
                弦值2 = response.getPropertyAsString(KEY2);

                SoapObject soapKey3 =(SoapObject)response.getProperty(KEY3);
                弦值4 = soapKey3.getPropertyAsString(KEY4);
                弦值5 = soapKey3.getPropertyAsString(KEY5);

                SoapObject soapKey6 =(SoapObject)soapKey3.getProperty(KEY6);
                SoapObject soapKey7 =(SoapObject)soapKey6.getProperty(KEY7);

                串value8 = soapKey7.getPropertyAsString(key8);

                串value9 = response.getPropertyAsString(key9);

                的System.out.println(值1 +,+值2 +,+值4 +,+值5 +,+ value8 +,+ value9);
            }
        }赶上(例外五){
            e.printStackTrace();
        }
 

I am using ksoap2 to consume a SOAP based web-service, and the format of the response I am getting is something like:

anyType{
    key1=value1;
    key2=value2;

    key3=anyType{

        key4=value4;
        key5=value5;
        key6= anyType{
                key7= anyType{
                    key8= value8;
                };
            };

    };

    key9=value9;
}

That is the JSON objects (if we assume that this is JSON) begin with anyType{ and end with }, keys and values are separated by =, and ; are field separators/statement terminators/whatever.

I tried to validate the response string using online validators but it failed. This points out that this is not a valid JSON object.

A similar example can be found in this question. But the accepted answer did not work for me because, well first the response string does not begin with { but with anyType{, and if I put anyType{ in the if condition, it still raises an exception next time when it encounters an anyType{ (a nested JSON object)

The second answer seems to work to some extent, but the problem is that my entire response string appears to come as a single property (since the propertyCount is 1), so when I print out the name or value of the property, the entire response string is printed.

I have googled it a lot and tried everything I could find. So I suppose I'll have to parse it myself.

My question is what is the best way to parse this kind of response.

Should I try to parse it using regex or should I convert the response string to a JSON format by replacing all occurances of anyType{ by {, = by :, ; by , etc. etc. , and then converting this string to a JSONObject by something like:

jsonObject= new JSONObject(responseString);

and then extract keys and values by something like:

Iterator itr= jsonObject.keys();

        while(itr.hasNext()) {
            String value = null; 

            String key= (String) itr.next();
            try {
                value= jsonObject.getString(key);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            Log.i(TAG, key + " : " + value); 

            // ......
        }


   import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

public class JSONPracticeOne {

    private static JSONObject jsonObject;

    private static String key;
    private static String value;

    public static void main(String[] args) {

        String responseString= " anyType{key1=value1;key2=value2;key3=anyType{key4=value4;key5=value5;key6=anyType{key7=anyType{key8=value8};};};key9=value9;}";

        responseString = responseString.replaceAll("anyType\\Q{\\E", "{");

        responseString = responseString.replaceAll("\\Q=\\E", ":");

        responseString = responseString.replaceAll(";", ",");

        responseString = responseString.replaceAll(",\\Q}\\E","}");

        //System.out.println(responseString); 

        //System.out.println();

        responseString= responseString.replaceAll("(:\\{)", "-"); //Replace :{ by -
        responseString= responseString.replaceAll("[:]", "\":\""); //Replace : by ":"
        responseString= responseString.replaceAll("-", "\":{\""); //Replace - back to :{

        //System.out.println(responseString);

        //System.out.println();

        responseString = responseString.replaceAll("[,]",",\"");

        //System.out.println(responseString); 

        //System.out.println();

        //String string= responseString.charAt(1) + "";  System.out.println("CHECHE " + string); 

        responseString = responseString.replaceFirst("[\\{]","{\"");

        //System.out.println(responseString);

        //System.out.println(); 

        //responseString= responseString.replaceAll("([^\\}],)","\","); // truncation

        responseString= responseString.replaceAll("(\\},)", "-"); // replace }, by -

        responseString= responseString.replaceAll(",","\","); //replace , by ",

        responseString = responseString.replaceAll("-","},"); // replace - back to },

        //System.out.println(responseString);

        //System.out.println();

        responseString = responseString.replaceAll("(?<![\\}])\\}","\"}");

        System.out.println(responseString);

        System.out.println("**********************************************************************************************\n\n");}}

OUTPUT:-

{
    "key1":"value1",
    "key2":"value2",
    "key3":{
        "key5":"value5",
        "key6":{
            "key7":{
                "key8":"value8"
            }
        },
        "key4":"value4"
    },
    "key9":"value9"
}

解决方案

Response is not JSON, it is JSON-like object and you can parse it using ksoap2's abilities.

In SoapObject.java, there are some methods like as follow:

 public Object getProperty(int index) {
        Object prop = properties.elementAt(index);
        if(prop instanceof PropertyInfo) {
            return ((PropertyInfo)prop).getValue();
        } else {
            return ((SoapObject)prop);
        }
    }

and

 /**
* Get the toString value of the property.
*
* @param index
* @return
*/
    public String getPropertyAsString(int index) {
        PropertyInfo propertyInfo = (PropertyInfo) properties.elementAt(index);
        return propertyInfo.getValue().toString();
    }

and

/**
* Get the property with the given name
*
* @throws java.lang.RuntimeException
* if the property does not exist
*/
    public Object getProperty(String name) {
        Integer index = propertyIndex(name);
        if (index != null) {
            return getProperty(index.intValue());
        } else {
            throw new RuntimeException("illegal property: " + name);
        }
    }

and

/**
* Get the toString value of the property.
*
* @param name
* @return
*/

    public String getPropertyAsString(String name) {
        Integer index = propertyIndex(name);
        if (index != null) {
            return getProperty(index.intValue()).toString();
        } else {
            throw new RuntimeException("illegal property: " + name);
        }
    }

etc..

And you can try parse your object like this:

try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject) envelope.getResponse();

            if (response.toString().equals("anyType{}") || response == null) {
                return;
            } else {
                String value1 = response.getPropertyAsString("key1");
                String value2 = response.getPropertyAsString("key2");

                SoapObject soapKey3 = (SoapObject) response.getProperty("key3");
                String value4 = soapKey3.getPropertyAsString("key4");
                String value5 = soapKey3.getPropertyAsString("key5");

                SoapObject soapKey6 = (SoapObject) soapKey3.getProperty("key6");
                SoapObject soapKey7 = (SoapObject) soapKey6.getProperty("key7");

                String value8 = soapKey7.getPropertyAsString("key8");

                String value9 = response.getPropertyAsString("key9");

                System.out.println(value1 + ", " + value2 + ", " + value4 + ", " + value5 + ", " + value8 + ", " + value9);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

这篇关于奇怪的SOAP响应,是JSON?如何分析呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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