Create JSONObject : JSON « Development « Android






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;
  }
}

   
    
    
  








Related examples in the same category

1.Parse Json string
2.Upload Image to Flickr with JSON
3.Flickr JSON Location
4.Using JSON
5.Json Client
6.Get List From Json Object
7.JSON: Get node value,
8.Data type conversion for JSON
9.Object to JSON
10.Wikidictionary
11.Browse through Wiktionary content
12.Utility class supporting the Facebook Object.