简体   繁体   中英

Extracting multiple array from JSON data

im still a novice android programmer ... Im trying to parse multiple array. This is the JSON data i received from my php code.

[{"mac":["00:12:17:E1:6B:07","90:94:E4:37:FD:C4"]},{"ap":["AP1","AP3"]}]

and my android code is

if (j3data != null && j3data.length() > 0) {

            JSONObject json_data; // creamos un objeto JSON
            try {
                json_data = j3data.getJSONObject(0); // leemos el primer
                                                        // segmento
                                                        // en nuestro caso el
                                                        // unico
                //ap = json_data.getString("ap"); //change please


                 JSONArray jArray = json_data.getJSONArray("mac");
                   List<String> list = new ArrayList<String>(); 
                   for (int i=0; i<jArray.length(); i++) { 
                       list.add( jArray.getString(i) ); 
                       Log.e("Output",jArray.getString(i)); 

                   }   

                //putting them into the list . a array 
                JSONArray j2Array = json_data.getJSONArray("ap");
                List<String> list2 = new ArrayList<String>(); 

                   for (int i=0; i<j2Array.length(); i++) { 
                       list2.add( j2Array.getString(i) ); 
                       Log.e("Output2",j2Array.getString(i)); 

                   }   
                   //Log.e("", list.get(0));

                Log.e("IndoorFragment ap --> ", ap);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return true;
        } else { // json obtenido invalido verificar parte WEB.
            Log.e("JSON3  ", "ERROR");
            return false;
        }

what im trying to do is to put the "mac" into a list and "ap" into another list. however using my current code, it only put "mac" values in but not the "ap" values . Please help! Thanks alot

Use bellow code to get your JSON data:

                JSONArray jArray=new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    if(i==0){
                        String str1=jArray.getJSONObject(i).getJSONArray("mac").getString(0);
                        String str2=jArray.getJSONObject(i).getJSONArray("mac").getString(1);
                        Log.e("Mac Result", "First: "+str1+" Second: "+str2);
                    }else if(i==1){
                        String str1=jArray.getJSONObject(i).getJSONArray("ap").getString(0);
                        String str2=jArray.getJSONObject(i).getJSONArray("ap").getString(1);
                        Log.e("Ap Result", "First: "+str1+" Second: "+str2);
                    }
                }

Where "result" is your json array string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM