Mega Code Archive

 
Categories / Android / Development
 

Create JSONObject

import java.io.File; import org.json.JSONObject; import org.json.JSONArray; import android.os.Environment; import android.util.Log; class FileUtil {   public static JSONObject GetDirJSON(File dir)   {       JSONObject jsonObj=new JSONObject();       try       {       jsonObj.put("name", dir.getName());       jsonObj.put("directory", dir.isDirectory());       jsonObj.put("url", dir.getAbsolutePath());     if(dir.equals(Environment.getExternalStorageDirectory()))     {       jsonObj.put("root", true);     }     else     {       jsonObj.put("root", false);       jsonObj.put("parent", dir.getParent());     }          if(dir.isDirectory())     {       File[] subFs=dir.listFiles();       JSONArray array=new JSONArray();       for(int i=0;i<subFs.length;i++)         array.put(subFs[i].getName());                   jsonObj.put("sub", array);     }            }catch(Exception e)       {         Log.d("Exec", e.getMessage());       }       Log.d("FileUtil",jsonObj.toString());     return jsonObj;   } }