Sol*_*ace 2 regex android json soap ksoap2
我正在使用ksoap2一个SOAP基础web-service,我得到的响应的格式是这样的:
anyType{
key1=value1;
key2=value2;
key3=anyType{
key4=value4;
key5=value5;
key6= anyType{
key7= anyType{
key8= value8;
};
};
};
key9=value9;
}
Run Code Online (Sandbox Code Playgroud)
那就是JSON objects(如果我们假设这是JSON)开始anyType{和结束},键和值是由=,并且;是字段分隔符/语句终止符/等等.
我试图验证响应字符串,online validators但失败了.这指出这不是有效的JSON object.
一个类似的例子可以发现在这个问题上.但接受的答案并没有为我工作,因为,第一响应字符串开头不是{但anyType{,如果我放anyType{的if条件下,它仍然抛出一个异常下一次,当它遇到一个anyType{(a nested JSON object)
第二个答案似乎在某种程度上有效,但问题是我的整个响应字符串看起来像一个属性(因为propertyCount是1),所以当我打印出属性的名称或值时,整个响应字符串打印.
我搜索了很多东西,并尝试了我能找到的一切.所以我想我必须自己解析它.
我的问题是解析这种反应的最佳方法是什么.
我应该尝试使用解析它regex 还是应该JSON format通过替换所有出现的anyType{by {,=by :,;by ,等来将响应字符串转换为a ,然后通过以下方式将此字符串转换为JSONObject:
jsonObject= new JSONObject(responseString);
Run Code Online (Sandbox Code Playgroud)
然后通过以下方式提取键和值:
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);
// ......
}
Run Code Online (Sandbox Code Playgroud)
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");}}
Run Code Online (Sandbox Code Playgroud)
输出: -
{
"key1":"value1",
"key2":"value2",
"key3":{
"key5":"value5",
"key6":{
"key7":{
"key8":"value8"
}
},
"key4":"value4"
},
"key9":"value9"
}
Run Code Online (Sandbox Code Playgroud)
响应不是JSON,它是类似JSON的对象,你可以使用ksoap2的能力解析它.
在SoapObject.java中,有一些方法如下:
public Object getProperty(int index) {
Object prop = properties.elementAt(index);
if(prop instanceof PropertyInfo) {
return ((PropertyInfo)prop).getValue();
} else {
return ((SoapObject)prop);
}
}
Run Code Online (Sandbox Code Playgroud)
和
/**
* 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();
}
Run Code Online (Sandbox Code Playgroud)
和
/**
* 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);
}
}
Run Code Online (Sandbox Code Playgroud)
和
/**
* 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);
}
}
Run Code Online (Sandbox Code Playgroud)
等等..
你可以尝试像这样解析你的对象:
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1899 次 |
| 最近记录: |