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" }]

そのjsonをWebサービスから取得する方法を知りたいです。私はそれについていくつかのチュートリアルを知っていますが、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」、「名前」を意味します....誰が私を助けてくれますか!

4

3 に答える 3

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

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

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 に答える