0

我有这样的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" }]

我想知道如何从 Web 服务中获取该 json。我知道一些关于那个的教程,但 json 总是这样:

    { "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”,“name”....谁能帮帮我,拜托!

4

3 回答 3

0

用户 JsonArray 作为基本元素。

喜欢:

JsonArray jArray = new JsonArray(jsonString);
for(int i=0;i<jArray.size();i++){
// Here you can get your json object

JsonObject jObj = jArray.getJSONObject(i);

// use this json object: jObj
String id = jObj.getString("id");
}

于 2012-12-03T08:52:49.343 回答
0

您可以在不解析 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;
            }


    }
于 2012-12-03T08:42:03.380 回答
0

假设您使用的是http://www.androidhive.info/2012/01/android-json-parsing-tutorial/之类的教程

尝试这个:

它说JSONObject json = jParser.getJSONFromUrl(url);更改为JSONArray jsonArray = jParser.getJSONFromUrl("url");

您还必须更改 to 的返回getJSONFromUrl(String url)类型JSONArray

于 2012-12-03T08:40:48.230 回答