Android在web服务上从json获取数据

Android在web服务上从json获取数据,android,json,web-services,Android,Json,Web Services,我有这样的json: [ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "+91 0000000000" }, { "id": "c201", "name": "Hero", "email": "ravi@gmail.com", "address":

我有这样的json:

[ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "+91 0000000000" },
   { "id": "c201", "name": "Hero", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "+90 0000000000" }]
    { "contacts": [ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x-country", "gender" : "male", "phone" : "00 000000" }, 
                    { "id": "c201", "name": "Johnny Depp", "email": "johnny_depp@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "00 000000", } ] }
我想知道如何从web服务获取json。我知道一些关于这方面的教程,但json总是这样:

[ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "+91 0000000000" },
   { "id": "c201", "name": "Hero", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "+90 0000000000" }]
    { "contacts": [ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x-country", "gender" : "male", "phone" : "00 000000" }, 
                    { "id": "c201", "name": "Johnny Depp", "email": "johnny_depp@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "00 000000", } ] }

但我不喜欢在“联系人”中获取数据,我只想在“联系人”中获取数据,它的意思是“id”、“姓名”。。。。谁能帮帮我

假设您使用的是类似于

试试这个:

其中表示
jsonobjectjson=jParser.getJSONFromUrl(url)更改为
JSONArray JSONArray=jParser.getJSONFromUrl(“url”)

您还必须将
getJSONFromUrl(字符串url)
的返回类型转换为
JSONArray

您可以在不解析Json的情况下使用ArrayList

private final String NAMESPACE = "http://tempuri.org/";
        private final String URL = "http://10.250.11.139/AirlineWebService/FlightsService.asmx";
        private final String SOAP_ACTION = "http://tempuri.org/GetAllFlights";
        public String METHOD_NAME = "";

     public static ArrayList<Product> getAllProducts(String category,int mode)
    {
        ArrayList<Product> listProducts = new ArrayList<Product>();
        String METHOD_NAME="GetAllProducts";
        Log.d("GetAllProducts", "Inside GetAllProducts function");

        try
        {
            SoapObject webRequest = new SoapObject(NAMESPACE, METHOD_NAME);

            PropertyInfo categ=new PropertyInfo();
            categ.setName("category");
            categ.setValue(category);
            categ.setType(String.class);
            webRequest.addProperty(categ);

            PropertyInfo pswd=new PropertyInfo();
            pswd.setName("mode");
            pswd.setValue(mode);
            pswd.setType(Integer.class);
            webRequest.addProperty(pswd);


            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(webRequest);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION + METHOD_NAME, envelope);
            Log.d("GetAllProducts - SOAP ACTION", SOAP_ACTION + METHOD_NAME);
            SoapObject response = (SoapObject)envelope.getResponse();
            int count=response.getPropertyCount();
            Log.d("GetAllCategories- Response", response.toString());

            for(int i=0;i<count; ++i)
            {
                  SoapObject p= (SoapObject) response.getProperty(i);

                   listProducts.add(new Product(Integer.parseInt(p.getProperty(0).toString()),p.getProperty(1).toString(), p.getProperty(2).toString(), p.getProperty(3).toString(), Double.parseDouble(p.getProperty(4).toString()), Double.parseDouble(p.getProperty(5).toString()),p.getProperty(6).toString()));

                   Log.d("GetAllCategories- Response", p.toString());
            }

            return listProducts;
        }catch (Exception e) {
                // TODO: handle exception

            return listProducts;
            }


    }
private final String NAMESPACE=”http://tempuri.org/";
私有最终字符串URL=”http://10.250.11.139/AirlineWebService/FlightsService.asmx";
私有最终字符串SOAP_ACTION=”http://tempuri.org/GetAllFlights";
公共字符串方法_NAME=“”;
公共静态ArrayList getAllProducts(字符串类别,int模式)
{
ArrayList listProducts=新的ArrayList();
字符串方法\u NAME=“GetAllProducts”;
Log.d(“GetAllProducts”,“内部GetAllProducts函数”);
尝试
{
SoapObject webRequest=新的SoapObject(名称空间、方法名称);
PropertyInfo categ=新的PropertyInfo();
类别设置名称(“类别”);
类别设定值(类别);
categ.setType(String.class);
webRequest.addProperty(categ);
PropertyInfo pswd=新的PropertyInfo();
pswd.setName(“模式”);
pswd.setValue(模式);
setType(Integer.class);
webRequest.addProperty(pswd);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
信封.setOutputSoapObject(webRequest);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
调用(SOAP\u操作+方法\u名称、信封);
Log.d(“GetAllProducts-SOAP动作”,SOAP动作+方法名称);
SoapObject响应=(SoapObject)信封.getResponse();
int count=response.getPropertyCount();
Log.d(“GetAllCategories-Response”,Response.toString());
对于(int i=0;i
用户JsonArray作为基本元素

比如:

JsonArray jArray=新的JsonArray(jsonString);

for(int i=0;icontacts是从web服务重新运行的JsonArray,为什么要删除它?)。使用androidcontacts Json中提供的Json解析器解析它无效。