在android开发中,如何解析web服务返回的ksoap2响应?

在android开发中,如何解析web服务返回的ksoap2响应?,android,xml-parsing,android-ksoap2,Android,Xml Parsing,Android Ksoap2,调用web方法后,返回一个XML字符串作为响应。 android端代码如下所示: SoapPrimitive response = (SoapPrimitive) ws.envelope.getResponse(); xml是这样的: <string xmlns="http://tempuri.org/"> [{"ID":"leo@gmail.com","float_latitude":22.338,"float_longitude":114.169}, {"ID

调用web方法后,返回一个XML字符串作为响应。 android端代码如下所示:

SoapPrimitive response = (SoapPrimitive) ws.envelope.getResponse();
xml是这样的:

<string xmlns="http://tempuri.org/">
[{"ID":"leo@gmail.com","float_latitude":22.338,"float_longitude":114.169},          {"ID":"emmy@emmy.com","float_latitude":22.33974,"float_longitude":114.169456},    {"ID":"bob@bob.com","float_latitude":22.3384857,"float_longitude":114.1691},    {"ID":"kay@kay.com","float_latitude":50,"float_longitude":100}]
</string>

[{“ID”:”leo@gmail.com,“浮动纬度”:22.338,“浮动经度”:114.169},{“ID”:emmy@emmy.com,“浮动纬度”:22.33974,“浮动经度”:114.169456},{“ID”:bob@bob.com,“浮动纬度”:22.3384857,“浮动经度”:114.1691},{“ID”:kay@kay.com,“浮动纬度”:50,“浮动经度”:100}]
我想去

  • 返回的id数
  • 将关于id的lat和long保存到我的局部变量

  • 哦,伙计。首先,您的响应不是XML,而是JSON。解析JSON的方法是首先构建一个通用对象

     public class yourObject{
    
      private String id; 
      private double lat;
      private double lng; 
     }
    
    从那里构建一个集合,一个arraylist就可以了

      ArrayList<yourObject> objects = new ArrayList<yourObject>(); 
    
    传入从服务器响应返回的字符串

    遍历数组

     for(int i = 0; i < myAwarry.length(); i++){
     JSONObject o = myAwarry.get(i); 
     yourObject obj = new yourObject(); 
     obj.setId(o.getString("ID")); 
     obj.setlat(o.getDouble("float_longitude")); 
     obj.setlng(o.getDouble("float_latitude")); 
     //add the obj to the collections of objects
      objects.add(obj); 
    
    for(int i=0;i
    }


    希望这对我有帮助

    它对我非常有效!我已经搜索了很多次如何解析ksoap2 xml,但都找不到解决方案,因为我弄乱了xml和json。再次感谢你。没问题,我很高兴能帮上忙。
     for(int i = 0; i < myAwarry.length(); i++){
     JSONObject o = myAwarry.get(i); 
     yourObject obj = new yourObject(); 
     obj.setId(o.getString("ID")); 
     obj.setlat(o.getDouble("float_longitude")); 
     obj.setlng(o.getDouble("float_latitude")); 
     //add the obj to the collections of objects
      objects.add(obj);