奇怪的SOAP响应,是JSON吗?怎么解析呢?

时间:2014-06-17 16:26:59

标签: android regex json soap ksoap2

我正在使用ksoap2来使用基于SOAP的{​​{1}},而我得到的格式的回复就像是:

web-service

这是anyType{ key1=value1; key2=value2; key3=anyType{ key4=value4; key5=value5; key6= anyType{ key7= anyType{ key8= value8; }; }; }; key9=value9; } (如果我们假设这是JSON objects)以JSON开头并以anyType{结尾,则键和值由{{1}分隔},}是字段分隔符/语句终结符/无论如何。

我尝试使用=验证响应字符串,但失败了。这指出这不是有效的;

可以找到类似示例 in this question。但接受的答案对我不起作用,因为首先响应字符串不是以online validators开头,而是以JSON object开头,如果我将{放入anyType{条件,下次遇到anyType{if

时仍会引发异常

第二个答案似乎在某种程度上有效,但问题是我的整个响应字符串看起来像是一个属性(因为propertyCount是1),所以当我打印出来时名称或属性值,打印整个响应字符串。

我搜索过很多东西并尝试了我能找到的一切。所以我想我自己必须解析它。

我的问题是解析此类响应的最佳方法是什么。

我是否应该尝试使用anyType{ 解析它,我应该将响应字符串转换为a nested JSON object,方法是将所有出现的regex替换为JSON format anyType{{ =: ;等等,然后通过以下内容将此字符串转换为JSONObject:

,

然后通过以下内容提取键和值:

jsonObject= new JSONObject(responseString);

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");}}

3 个答案:

答案 0 :(得分:5)

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

SoapObject.java中,有一些方法如下:

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

 /**
* 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();
    }

/**
* 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);
        }
    }

/**
* 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);
        }
    }

等。

你可以尝试解析你的对象:

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();
        }

答案 1 :(得分:2)

试试这个我觉得它会起作用

          SoapObject response = (SoapObject) envelope.getResponse();

          int cols = response.getPropertyCount();

            for (int i = 0; i < cols; i++) {
                Object objectResponse = (Object) response.getProperty(i);




                SoapObject r =(SoapObject) objectResponse;

             String   key1=(String) r.getProperty("key1").toString();

                // Get the rest of your Properties by 
                // (String) r.getProperty("PropertyName").toString();

            }

答案 2 :(得分:1)

  1. 将您的响应字符串转换为soap对象。
  2. 对soap对象执行属性计数并迭代计数。
  3. 对于每个属性,强制转换为Object并检查Object是否为类类型Soap。如果Object是soap类类型,则转换为Soap对象并从步骤2重做,否则将该属性作为字符串值检索。

    SoapObject result=(SoapObject)responseString;
    if (result.getPropertyCount() > 0){
        Object obj = result.getProperty(0);
        if (obj!=null && obj.getClass().equals(SoapObject.class)){
            SoapObject j = (SoapObject)obj;
        }
    }   
    
    for (int i = 0; i < j.getPropertyCount(); i++) {
        Object obj0 = j.getProperty(i);                     
        if (obj0!=null && obj0.getClass().equals(SoapObject.class)){
            SoapObject j0 =(SoapObject) j.getProperty(i);
            for (int i0 = 0; i0 < j0.getPropertyCount(); i0++) {
                Object obj1 = j0.getProperty(i0);
                if (obj1!=null && obj1.getClass().equals(SoapObject.class)){
                    SoapObject j1 =(SoapObject) j0.getProperty(i0);
                    //TODO retrieve the properties for this soap object
                }else{
                    // retrieve soap property as string
                    String keyValue = obj1.toString()
                }   
            }
        }else{
            // retrieve soap property as string
            String keyValue0 = obj0.toString();
        }
    }