Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.75 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. JSon Read inner array
  2. "Manager":
  3. [
  4.     {
  5. "name" : "Sample item",
  6. "icon" : "http://www.test.com/icon.png",
  7. "link" : "http://www.test.com/sample.zip",
  8. "summary" : "sample summary here",
  9. "screenshots": [
  10.     "http://test.com/link2screenshot1.png",
  11.     "http://test.com/link2screenshot2.png"
  12.     ]
  13.     },
  14.     {
  15. "name" : "Sample item2",
  16. "icon" : "http://www.test.com/icon2.png",
  17. "link" : "http://www.test.com/sample2.zip",
  18. "summary" : "sample summary here2",
  19. "screenshots": [
  20.     "http://test.com/2/link2screenshot1.png",
  21.     "http://test.com/2/link2screenshot2.png"
  22.     ]
  23. }
  24. ]
  25.        
  26. public ArrayList<String> JSONInner(int count, String url, String outer, String inner){
  27.     ArrayList<String> linkArrayList = new ArrayList<String>();
  28.     URL u = null;
  29.     StringBuilder FileData = null;
  30.     try {
  31.         u = new URL(url);
  32.         String InputData =       null;
  33.         InputStream is =         null;
  34.         DataInputStream dis =    null;
  35.         FileData = new StringBuilder();
  36.         is = u.openStream();
  37.         dis = new DataInputStream(new BufferedInputStream(is));
  38.         while ((InputData = dis.readLine()) != null) {
  39.             FileData.append(InputData);
  40.         }
  41.         is.close();
  42.     } catch (MalformedURLException e1) {
  43.         e1.printStackTrace();
  44.     } catch (IOException e) {
  45.         e.printStackTrace();
  46.     }
  47.     JSONObject json = null;
  48.     try {
  49.         json = new JSONObject(FileData.toString());
  50.     } catch (JSONException e1) {
  51.         e1.printStackTrace();
  52.     }
  53.     if(FileData.toString() != null) {
  54.         try {
  55.             JSONArray managerArray = json.getJSONArray(outer);
  56.             JSONObject managerObject = managerArray.optJSONObject(count);
  57.             JSONArray linkArray = managerObject.getJSONArray(inner);
  58.             for(int subCount = 0, maxSubCount = linkArray.length(); subCount < maxSubCount; subCount++){
  59.                 linkArrayList.add( linkArray.getString( subCount ));
  60.             }
  61.         }catch (JSONException e){
  62.             e.printStackTrace();
  63.         }
  64.     }
  65.     return linkArrayList;
  66. }
  67.        
  68. //Init
  69. ArrayList<String> linkArrayList = new ArrayList<String>();
  70. JSONArray managerArray = new JSONArray(yourString);
  71.  
  72. //Iterate over the top-level array
  73. for( int count = 0, max = managerArray.length(); count < max; count++ ) {
  74.  
  75.     //Get the current JSONObject & the link array
  76.     JSONObject managerObject = managerArray.optJSONObject(count);
  77.     JSONArray linkArray = managerObject.getJSONArray("screenshots");
  78.  
  79.     //Iterate over the screenshots
  80.     for(
  81.         int subCount = 0, maxSubCount = linkArray.length();
  82.         subcount < maxSubCount;
  83.         subCount++
  84.     ) {
  85.  
  86.         //Let's store the String
  87.         linkArrayList.add( linkArray.getString( subCount );
  88.  
  89.     }
  90.  
  91. }
  92.        
  93. int count = 0; //The 0 should be the index of your item