0

Hi I'm working on a dynamic android application and i have the following json. Reading each line is fine i've figured out how to do this in java (keep in mind i just started working with json yesterday) how can i read the inner array of say sample item2 to an ArrayList in java i need some kind of loop to add to a string array the screen shot links. Thank you for any help with my problem.

 "Manager": 
[
    { 
"name" : "Sample item", 
"icon" : "http://www.test.com/icon.png", 
"link" : "http://www.test.com/sample.zip", 
"summary" : "sample summary here", 
"screenshots": [ 
    "http://test.com/link2screenshot1.png", 
    "http://test.com/link2screenshot2.png"
    ] 
    },
    { 
"name" : "Sample item2", 
"icon" : "http://www.test.com/icon2.png", 
"link" : "http://www.test.com/sample2.zip", 
"summary" : "sample summary here2", 
"screenshots": [ 
    "http://test.com/2/link2screenshot1.png", 
    "http://test.com/2/link2screenshot2.png"
    ] 
}
]

===EDIT===

Alright here is the final solution the following method will read a json from a url and pull what i want thanks to ninetwozero

public ArrayList<String> JSONInner(int count, String url, String outer, String inner){
    ArrayList<String> linkArrayList = new ArrayList<String>();
    URL u = null;
    StringBuilder FileData = null;
    try {
        u = new URL(url);
        String InputData =       null;
        InputStream is =         null;
        DataInputStream dis =    null;
        FileData = new StringBuilder();
        is = u.openStream();
        dis = new DataInputStream(new BufferedInputStream(is));
        while ((InputData = dis.readLine()) != null) {
            FileData.append(InputData);
        }
        is.close();
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject json = null;
    try {
        json = new JSONObject(FileData.toString());
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    if(FileData.toString() != null) {
        try {
            JSONArray managerArray = json.getJSONArray(outer);
            JSONObject managerObject = managerArray.optJSONObject(count);
            JSONArray linkArray = managerObject.getJSONArray(inner);
            for(int subCount = 0, maxSubCount = linkArray.length(); subCount < maxSubCount; subCount++){
                linkArrayList.add( linkArray.getString( subCount ));
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
    return linkArrayList;
}
2
  • Are you doing it by hand or are you using something like jackson ? wiki.fasterxml.com/JacksonInFiveMinutes Commented Feb 4, 2012 at 16:38
  • @redben doing it by hand currently i can post the method i'm using to get the lines i need if that's needed, however it is messy Commented Feb 4, 2012 at 16:41

1 Answer 1

1

I'd do it the following way:

//Init
ArrayList<String> linkArrayList = new ArrayList<String>();
JSONArray managerArray = new JSONArray(yourString);

//Iterate over the top-level array
for( int count = 0, max = managerArray.length(); count < max; count++ ) {

    //Get the current JSONObject & the link array
    JSONObject managerObject = managerArray.optJSONObject(count);
    JSONArray linkArray = managerObject.getJSONArray("screenshots");

    //Iterate over the screenshots
    for( 
        int subCount = 0, maxSubCount = linkArray.length(); 
        subcount < maxSubCount; 
        subCount++ 
    ) {

        //Let's store the String
        linkArrayList.add( linkArray.getString( subCount );

    }

}

Let me know if this is what you're looking for. :-)

Edit: However, this would give you ALL the links - if you just want the one for array item #2, then you can remove the outer loop and declare the following:

int count = 0; //The 0 should be the index of your item
Sign up to request clarification or add additional context in comments.

7 Comments

just so i'm clear managerArray = new JSONArray(yourString) would yourString be equal to say "Sample item2", Also where would i tell it the json is "Manager" or is that not needed? thank you for the reply
nevermind i assume now that JSONArray managerArray = new JSONArray(yourString) would be JSONArray managerArray = new JSONArray("Manager") and then if i wanted the second items screen shots would i set count equal to 1 since it's 0 index? although then i'd loop over the first one as well which i don't want the screen shots for?
sorry for the third comment i keep losing the ability to edit but i think i completely understand removing the loop now and gonna give it a run i'll let you know how it goes thanks
Last comment i promise :p how would I pass it the full json as well lol sorry very very new to working with json
Hey, sorry about not seeing this earlier - the "yourString" corresponds to the JSON-string in your original post - in other words, what you download from a web server. || The JSONArray is 0-indexed, so you'll have to set int count = 1 if you were to get element #2 (without looping past #1 first). I'm not 100% sure what you mean by "pass it the whole JSON as well", as it actually handles that "as-is" right now. :-)
|

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.