Android应用程序解析json数据

时间:2014-05-06 08:25:58

标签: android parsing soap android-json

你好,我有这个代码我想解析这个数据到对象现在我得到一个anyType字符串

我想分别获取描述对象纬度对象和经度对象

@SuppressLint("NewApi") 
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/xxxx/xxxx";
private static final String METHOD_NAME = "xxxxxx";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:52564/xxxx.svc/soap";    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 

    TextView textView = new TextView(this);

    setContentView(textView);

    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransport = new HttpTransportSE(URL);

    try
    {
        httpTransport.call(SOAP_ACTION, envelope);  
        Object result = (Object)envelope.getResponse();
        textView.setText(result.toString());
    }
    catch (Exception exception)
    {
        textView.setText(exception.toString());
    }
}
}

3 个答案:

答案 0 :(得分:0)

将json解析为对象的最简单方法是使用对象映射器

    ObjectMapper om = new ObjectMapper();
    YourClass object = om.readValue(jsonText, YourClass.class); 

这个对象映射器来自jackson:

http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/ObjectMapper.html

答案 1 :(得分:0)

您可以使用json对象来处理响应。

以下是代码摘录:

JSONObject obj = new JSONObject(envelope.getResponse().toString());
String Latitude = obj.getString("Latitude");
String Longitude = obj.getString("Longitude");

如果你的json看起来像这样:

{
  Latitude: "1.00",
  Longitude: "1.00"
}

根据你的评论你的json看起来像这样:

[
  {
    "Description": "1",
    "Latitude": 0.369,
    "Longitude": 1.258
  },
  {
    "Description": "2",
    "Lati‌​tude": 1.369,
    "Longitude": 3.258
  },
  {
    "Description": "3",
    "Latitude": 2.369,
    "Longitude": 2.258
  }
]

您可以像这样提取值:

JSONArray obj = new JSONArray(envelope.getResponse().toString());
String[][] values = new String[obj.length()][3];
for(int i = 0; i < obj.length(); i++)
{
    values[i][0] = obj.getJSONObject(i).getString("Description");
    values[i][1] = obj.getJSONObject(i).getString("Latitude");
    values[i][2] = obj.getJSONObject(i).getString("Longitude");
}

这里可能有错误,我在这里快速写下来。
但它是你需要理解的概念。

答案 2 :(得分:0)

你的json数据是

[
    {
        "Description": "1",
        "Latitude": 0.369,
        "Longitude": 1.258
    },
    {
        "Description": "2",
        "Latitude": 1.369,
        "Longitude": 3.258
    },
    {
        "Description": "3",
        "Latitude": 2.369,
        "Longitude": 2.258
    }
]

为数据创建一个类,如下所示

public class Detail {
    String description;
    Double lat;
    Double longi;

    public Detail(String description, Double lat, Double longi) {
        this.description=description;
        this.lat=lat;
        this.longi=longi;
    }
}

arraylist

制作Deatail之后的

List<Detail> detailList=new ArrayList<Detail>();

并执行以下操作

String jsondata = loadJSONFromAsset();//load data and assign as json string
        JSONArray primaryArray;
        try {
            primaryArray = new JSONArray(jsondata);

            for (int i = 0; i < primaryArray.length(); i++) {

                JSONObject jObject = primaryArray.getJSONObject(i);
                String description = (String) jObject.getString("Description");
                Log.d("Description", description);
                Double latitude =  jObject.getDouble("Latitude");
                Log.d("Latitude", latitude+"");
                Double longitude = jObject.getDouble("Longitude");
                Log.d("Longitude", longitude+"");

                detailList.add(new Detail(description, latitude, longitude));
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

现在您可以从arraylist

中获取所有详细数据