2014-06-17 56 views
2

我使用ksoap2消耗SOAP基于web-service,响应我得到的格式是一样的东西:奇怪的SOAP响应,它是JSON吗?如何解析它?

anyType{ 
    key1=value1; 
    key2=value2; 

    key3=anyType{ 

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

    }; 

    key9=value9; 
} 

这是JSON objects(如果我们认为这是JSON)开头anyType{并以}结尾,键和值由=分开,并且;是字段分隔符/语句结束符/不管。

我试图验证使用online validators响应字符串,但它失败。这指出这不是有效的JSON object

A 类似的例子可以发现in this question。但接受的答案没有为我工作,因为,第一响应字符串不{anyType{开始,如果我在if条件而anyType{,它仍然抛出一个异常,当它遇到一个anyType{下一次( a nested JSON object

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

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

我的问题是解析这种回应的最佳方式是什么。

我应该尝试使用regex我应由{更换的anyType{所有出现转换的响应串到JSON format=通过:;通过,等等,等等来分析它,然后转换该字符串一个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");}} 

OUTPUT: -

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

我不知道什么任何人,但你得到不JSON –

+0

@IllegalArgument谢谢你,我已经达到了同样的结论后说很多辛苦,但问题(在问题中显示为粗体字)仍然是解析它的最佳方式? – Solace

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

不错,你打我 – ramizmoh

+0

您的解决方案非常简单易用,说实话我更倾向于使用它,但试图使用它会引发'运行时异常:非法属性';并查看'[SoapObject](http s://github.com/mosabua/ksoap2-android/blob/master/ksoap2-base/src/main/java/org/ksoap2/serialization/SoapObject.java)class',我发现属性的索引出现'null' – Solace

1
  1. 你要把你的回应字符串作为SOAP对象。
  2. 为肥皂对象做一个属性计数并迭代计数。
  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(); 
        } 
    } 
    
+0

非常感谢。虽然我花了一些时间来彻底了解它,因为我是一个新手,但这很棒;除了有一个问题。在我想要制作的应用程序中,网络服务(取决于用户请求的内容)会有不同级别的嵌套响应。例如在你的代码中,你在另一个嵌套for循环,在我调整你的代码来测试真正的web服务响应的代码中,我使用了嵌套在另一个中的一个属性,嵌套在另一个中,在另一个中嵌套,这是5倍嵌套)...... – Solace

+0

...这是一种基于一个肥皂请求的响应类型。现在用户将有不同的选项来选择他们想要请求的web服务。根据请求,响应将具有不同的属性,因此我们无法预测这些属性将嵌套到其他属性中的程度。如果你能为我提供一些建议,我会很感激。再次感谢您的回答。 – Solace

+0

@Zarah:不幸的是,由于KSOAP本身并没有使用wsdl,你将不得不非常了解Web服务和它的响应。你已经体验过的东西;因此提供的代码是为了帮助理解。你可以看一下代码生成器www.wsdl2code.com;尽管从我的经验来看,为了映射Web服务响应而生成的类在我的情况下被错误地映射了。 – ramizmoh

1

试试这个,我认为它会工作

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

      }