我使用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"
}
我不知道什么任何人,但你得到不JSON –
@IllegalArgument谢谢你,我已经达到了同样的结论后说很多辛苦,但问题(在问题中显示为粗体字)仍然是解析它的最佳方式? – Solace
发布您的整个响应解析代码是依赖于它我假设最终代码将导致类似字符串操作 –