2012-12-29 28 views
0

我正在使用包含JSON的字符串,该字符串由ASP Web服务传递给Android。我收到我的Android应用程序的字符串如下:如何分离嵌入在Android字符串中的JSON对象?

GetCustomerListResponse{GetCustomerListResult=[{"VehicleID":"KL-9876","VehicleType":"Nissan","VehicleOwner":"Sanjiva"}]; } 

说我想从JSON字符串的车型,我该怎么做呢?

我的完整的Android代码如下:

package com.example.objectpass; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.TextView; 
import org.ksoap2.*; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.*; 

public class MainActivity extends Activity { 
    TextView resultA; 
    Spinner spinnerC; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     String[] toSpinnerSum; 
     toSpinnerSum = new String[9]; 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     spinnerC = (Spinner) findViewById(R.id.spinner1); 
     resultA = (TextView) findViewById(R.id.textView2); 

     final String NAMESPACE = "http://tempuri.org/"; 
     final String METHOD_NAME = "GetCustomerList"; 
     final String SOAP_ACTION = "http://tempuri.org/GetCustomerList"; 
     final String URL = "http://192.168.1.100/WebService4/Service1.asmx"; 

     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
     SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 
     soapEnvelope.dotNet = true; 
     soapEnvelope.setOutputSoapObject(Request); 
     AndroidHttpTransport aht = new AndroidHttpTransport(URL); 

     try { 
      aht.call(SOAP_ACTION, soapEnvelope); 
      SoapObject response = (SoapObject) soapEnvelope.bodyIn; 

      resultA.setText(response.toString()); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

任何帮助将不胜感激。谢谢

回答

1

解析当前JSON字符串为:

//Convert String to JsonArray 
JSONArray jArray = new JSONArray(response.toString()); 

for(int i=0;i<jArray.length();i++){ 
    // get json object from json Array 
    JSONObject jsonobj = jArray.getJSONObject(i); 

    //get VehicleType from jsonObject 
    String str_VehicleType=jsonobj.getString("VehicleType"); 

    //get VehicleOwner from jsonObject 
    String str_VehicleOwner=jsonobj.getString("VehicleOwner"); 

} 

,并获得更多信息,我们如何解析Android中的josn串看到

http://www.technotalkative.com/android-json-parsing/

+0

嘿,我做了如下变化: JSONArray jArray = new JSONArray (response.toString()); \t \t \t对(INT I = 0; I Kasanova

+0

@najjaseven:你将需要把一些日志和检查日志,你是从json或不str_VehicleType变量中获得价值。 –

+0

@najjaseven:你在response.toString()中得到了什么字符串? –

1

这里,试试这个:

final int jsonBeginIdx = response.firstIndexOf("="); 
final int jsonEndIdx = response.lastIndexOf(";"); 

if(jsonBeginIdx > 0 && jsonEndIdx > jsonBeginIdx) { 
    final String jsn = response.substring(jsonBeginIdx + 1, jsonEndIdx); 
    final JSONObject jsonObj = new JSONObject(json); 
} else { 
    // deal with malformed response here 
} 
相关问题