如何解析Android中此Web服务响应? [英] How to parse this Web service response in Android?
问题描述
我使用KSOAP2调用来自Android的应用程序提供.NET Web服务,并从Web服务的响应是按以下格式
I am using KSOAP2 to call a .NET webservice from android application,and the response from the web service is in the following format
anyType{
UserName=anyType{};
Password=anyType{};
ApplicationCode=JOB;
ActionType=Query;
MessageParameters=anyType{Parameters=anyType{}; };
TableData=anyType{TableNo=167;
TableName=Job;
DataRows=
anyType{
DataRow=
anyType{
DataRowValues=
anyType{
DataRowValue=
anyType{
FieldNo=1;
FieldName=No.;
PrimaryKey=true;
FieldType=Code20; DataValue=DEERFIELD, 8 WP;
};
DataRowValue=
anyType
{
FieldNo=3;
FieldName=Description;
PrimaryKey=false;
FieldType=Text50;
DataValue=Setting up Eight Work Areas;
};
DataRowValue=
anyType
{
FieldNo=4;
FieldName=Description 2;
PrimaryKey=false;
FieldType=Text50;
DataValue=anyType{};
};
};
};
};
};
};
ResponseForRequest=GETTABLEDATA;
CustomIdentifier=TestBB;
Applications=anyType{};
Forms=anyType{};
Menu=anyType{};
}
我不知道这个响应的格式,我不知道如何分析这种反应来获得特定result.Any人知道这件事,请帮助我。
I am not aware about the format of this response and i don't know how to parse this response to get particular result.Any one knows about it please help me.
注:我手动格式化的这种反应的理解
Note: i manually formatted this response for your understanding.
推荐答案
其实这是一个已知的格式,如果你知道在这个格式的Java Script.These数据是逸岸 JSON对象
和 JSON数组
的。我希望你用 KSOAP2库
。所以这里是你如何解析这个结果。
Actually this a known format if you know Java Script.These data in this format are infact JSON Object
's and JSON Array
's. I hope you are using the KSOAP2 library
.So here is how you can parse this result.
例如:
private Bundle bundleResult=new Bundle();
private JSONObject JSONObj;
private JSONArray JSONArr;
Private SoapObject resultSOAP = (SoapObject) envelope.getResponse();
/* gets our result in JSON String */
private String ResultObject = resultSOAP.getProperty(0).toString();
if (ResultObject.startsWith("{")) { // if JSON string is an object
JSONObj = new JSONObject(ResultObject);
Iterator<String> itr = JSONObj.keys();
while (itr.hasNext()) {
String Key = (String) itr.next();
String Value = JSONObj.getString(Key);
bundleResult.putString(Key, Value);
// System.out.println(bundleResult.getString(Key));
}
} else if (ResultObject.startsWith("[")) { // if JSON string is an array
JSONArr = new JSONArray(ResultObject);
System.out.println("length" + JSONArr.length());
for (int i = 0; i < JSONArr.length(); i++) {
JSONObj = (JSONObject) JSONArr.get(i);
bundleResult.putString(String.valueOf(i), JSONObj.toString());
// System.out.println(bundleResult.getString(i));
}
}
起初,我有很多的麻烦,这种类型的数据,但最后我得到了这一切working.From然后我一直在使用this.I希望这可以帮助您解决问题。
Initially i had a lot of trouble with this kind of data but finally i got it all working.From then I have been using this.I hope this helps you solve your problem.
这篇关于如何解析Android中此Web服务响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!