Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Still working on this part of my script to parse my JSON jsonArray = new JSONArray(jsonData); and then adding it to a arraylist "myJSONArray"

I am not sure how the for loop would work to add my elements from jsonArray into myJSONArray (arrayList)

If I am not clear please let me know and if there is any info I am missing just ask. Thanks in advance.

JSON data:

{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}

image_data class:

public class image_data {


    public int id;
    public String name;
    public String thumb;
    public String path;


}

ShowThumb class:

public class showThumb extends Activity{



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridlayout);

         Bundle bundle = getIntent().getExtras();
         String jsonData = bundle.getString("jsonData");


         JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(jsonData);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ArrayList<image_data> myJSONArray = new ArrayList<image_data>();

         for (int i=0; i<jsonArray.length(); i++) {
               **//This is where I think I create the seperate arrays of id name thumb and path**
             myJSONArray.add(new image_data());

         }


GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this,image_data));

    }
}
share|improve this question
 
Show the JSON output..or at least part of it to have representation what objects need to be parsed. –  Nikola Despotoski Jul 26 '11 at 3:18
 
Edit: added the JSON data. –  Denoteone Jul 26 '11 at 3:22
 
It would probably make more sense to arrange for the JSON data to be the other way around - an array of objects, rather than an object of arrays. –  Karl Knechtel Jul 26 '11 at 3:32
 
@Karl The JSON data came out of my MySQL query -> json_encode that way. Would I alter my query to pull the data in a different order? –  Denoteone Jul 26 '11 at 3:35
add comment

2 Answers

up vote 2 down vote accepted

How you iterate and add

 for(int i=0;i<jsonArray.length;i++)
    {    
         JSONObject json_data = jArray.getJSONObject(i);
         imgnfo.id =  json_data.getInt("id_key");
         imgnfo.name = json_data.getString("name_key");
         imgnfo.thumb = json_data.getString("thumb_key");
         imgnfo.info = json_data.getString("info_key");
         myArray.add(new image_data());
    }

Just add proper keys names. I didn't know them.

share|improve this answer
 
Thanks I am see there are many ways to do the same thing. +1 –  Denoteone Jul 26 '11 at 3:34
add comment

Yo should post what the json data looks like. Assuming the json data looks something like

...,{
    "image_data": {
        "name": "image name","thumb":"thumbpath","path":"path"}
},{
    "image_data": {
        "name": "image name 2","thumb":"thumbpath 2","path":"path 2"}
}

Then you would extract the values like so:

    ArrayList<image_data> imageArray = new ArrayList<image_data>();

         for (int i=0; i<jsonArray.length(); i++) {
             image_data img = new image_data();

             Object jo = jsonArray.get(i);

             image_data.name = jo.name;
             image_data.thumb = jo.thumb;
             image_data.path = jo.path;

             imageArray.add(new image_data());

         }

Consider using getters for you image_data class. Also consider renaming it to ImageData

share|improve this answer
 
sorry I edited my post and added the json layout. +1 thanks your post was helpful. –  Denoteone Jul 26 '11 at 3:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.