奇怪的 SOAP 响应,是 JSON 吗?如何解析它? [英] Weird SOAP response, is it JSON? How to parse it?
问题描述
我正在使用 ksoap2
来使用基于 SOAP
的 web-service
,以及响应的格式我得到的是这样的:
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;
}
这就是 JSON 对象
(如果我们假设这是 JSON
)以 anyType{
开头并以 }<结尾/code>,键和值由
=
分隔,;
是字段分隔符/语句终止符/随便什么.
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.
我尝试使用 online validators
验证响应字符串,但失败了.这指出这不是有效的 JSON 对象
.
I tried to validate the response string using online validators
but it failed. This points out that this is not a valid JSON object
.
可以找到类似示例在这个问题中.但是接受的答案对我不起作用,因为首先响应字符串不是以{
开头而是以anyType{
开头,如果我将 anyType{
放在 if
条件中,下次遇到 anyType{
(嵌套的 JSON 对象)时仍会引发异常
)
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
)
第二个答案 似乎在某种程度上有效,但问题是我的整个响应字符串似乎作为一个属性出现(因为 propertyCount 是 1),所以当我打印出属性的名称或值,将打印整个响应字符串.
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.
我的问题是解析这种响应的最佳方式是什么.
我应该尝试使用 regex
解析它 还是 我应该通过替换所有出现的情况将响应字符串转换为 JSON 格式
anyType{
by {
, =
by :
, ;
by ,
等等,然后通过以下方式将此字符串转换为 JSONObject:
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("**********************************************************************************************
");}}
输出:-
{
"key1":"value1",
"key2":"value2",
"key3":{
"key5":"value5",
"key6":{
"key7":{
"key8":"value8"
}
},
"key4":"value4"
},
"key9":"value9"
}
推荐答案
响应不是 JSON,它是类似 JSON 的对象,您可以使用 ksoap2 的能力解析它.
Response is not JSON, it is JSON-like object and you can parse it using ksoap2's abilities.
在 SoapObject.java,有如下几种方法:
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);
}
}
和
/**
* 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);
}
}
等等.
你可以尝试像这样解析你的对象:
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屋!