0

I am a little new to android/java. I am trying to pass JSON values into a list and then into a multidimensional array. I am not having much success.

2 questions, 1) How would I load all of the variables in a json array into children[][]? 2) How do you view children[][] in Log.i

Herei s my code:

List<String> cList = new ArrayList<String>();
String customer_name, customer_title, customer_postal_code, customer_city, customer_state, customer_street_address;   
ArrayList<String> cTitle, cClubName, cPostalCode, cCity, cState, cStreet = new ArrayList<String>();    
public String[][] children = null;

//... onCreate method, HTTP Connection, StringBuilder, etc. These work fine... // Pass data into array

try{
    JSONArray jArray = new JSONArray(result);
    JSONObject jData=null;


String[][] children = new String[jArray.length()][6];

    for(int i=0;i<jArray.length();i++){


            jData = jArray.getJSONObject(i);

            customer_name=jData.getString("customer_name");     
            Log.i("JSON ", "customer_name LOG " + customer_name);
            cList.add(customer_name);

            customer_title=jData.getString("event_title");
            Log.i("JSON ", "customer_title LOG " + customer_title);
            cList.add(customer_title);

            customer_street_address=jData.getString("customer_street_address");
            Log.i("JSON ", "customer_Id LOG " + customer_street_address);
            cList.add(customer_street_address);


            customer_city=jData.getString("customer_city");
            Log.i("JSON ", "customer_city LOG " + customer_city);
            cList.add(customer_city);



            customer_state=jData.getString("customer_state");   
            Log.i("JSON ", "customer_state LOG " + customer_state);
            cList.add(customer_state);


            customer_postal_code=jData.getString("customer_postal_code");               
            Log.i("JSON ", "customer_postal_code LOG " + customer_postal_code);
            cList.add(customer_postal_code);

            for(int ic = 0; ic < cList.size(); ic++) {      
                Log.i("jData ", "length " + jData.length());

                children[i][ic] = (String) cList.get(ic);

            }

            Log.i("Child Array", "Children array LOG " + children);




    }


    }catch(JSONException e1){
        Toast.makeText(getBaseContext(), "No customers Found", Toast.LENGTH_LONG).show();
    }catch (ParseException e1){
        e1.printStackTrace();
    }

}

1
  • Can you get an example of a json? I will try to get you more flexible and good looking solution Commented Mar 30, 2012 at 8:16

2 Answers 2

1

If I understood your code correctly you don't need cList. Something like that should do the work

String[][] children = new String[jArray.length()][6];

    for(int i=0;i<jArray.length();i++){

            jData = jArray.getJSONObject(i);

            customer_name=jData.getString("customer_name");     
            Log.i("JSON ", "customer_name LOG " + customer_name);
            children[i][0] = customer_name;


            customer_title=jData.getString("event_title");
            Log.i("JSON ", "customer_title LOG " + customer_title);
            children[i][1] = event_title;

            customer_street_address=jData.getString("customer_street_address");
            Log.i("JSON ", "customer_Id LOG " + customer_street_address);
            children[i][2] = customer_street_address;

            customer_city=jData.getString("customer_city");
            Log.i("JSON ", "customer_city LOG " + customer_city);
            children[i][3] = customer_city;

            customer_state=jData.getString("customer_state");   
            Log.i("JSON ", "customer_state LOG " + customer_state);
            children[i][4] = customer_state;


            customer_postal_code=jData.getString("customer_postal_code");               
            Log.i("JSON ", "customer_postal_code LOG " + customer_postal_code);
            children[i][5] = customer_postal_code;
    }

Make sure your JSON data is well-formed to avoid exceptions. To view children[][] you can just iterate twice on your multidimentional array and do Log.i("MyTag", "Value: " + children[i][j]);

Sign up to request clarification or add additional context in comments.

Comments

0
String[][] data = new String[jsonArray.length][];

for(int i = 0; i<jsonArray.length; i++){
     data[i] = ((ArrayList)jsonArray).get(i).toArray(new String[((ArrayList)jsonArray).get(i).size])
}

for printing in log

Log.d("array", data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.