Search code examples
javajsonworklight-adapters

Returning JSON from Java to Worklight Adapter


I have one class FileHandling in which there is one method called readAllLayouts which will read the all files from specified folder and return the content as JSONArray to worklight adapter.

I have got this type of Error when invoking worklight procedure:

{
   "errors": [
      "Evaluator: Java class \"org.json.simple.JSONArray\" has no public instance field or method named \"isSuccessful\"."
   ],
   "info": [
   ],
   "isSuccessful": false,
   "warnings": [
   ]
}

and this is the code of my Java method:

public static JSONArray readAllLayoutFiles ( ){

    File layoutDir = new File( LAYOUT_PARENT_DIR );

    String allFiles[] = layoutDir.list();
    System.out.println( "All Files Length : " + allFiles.length );
    JSONObject obj = null;//new JSONObject[ allFiles.length ];

    JSONArray retArr = new JSONArray();

    for ( String f : allFiles ){
        obj= new JSONObject();
        obj.put( "layoutname", f.replaceAll (".txt", "" ) );
        obj.put( "layouthtml", readLayoutFile ( f ) );
        retArr.add(obj);
    }
    obj= new JSONObject();
    obj.put( "isSuccessful", true );
    retArr.add(obj);

    System.out.println( retArr.toString() );
    return retArr;
}

Any help would be appreciated.


Solution

  • Because of the problem I have changed my code that will working, but now It is not returning JSON but returning String that contains JSON.

    Here is the code and Working Fine :

    public static String readAllLayoutFiles ( ){
    
        File layoutDir = new File( LAYOUT_PARENT_DIR );
    
        String allFiles[] = layoutDir.list();
        System.out.println( "All Files Length : " + allFiles.length );
        JSONObject obj = null;//new JSONObject[ allFiles.length ];
    
        JSONArray retArr = new JSONArray();
    
        for ( String f : allFiles ){
            obj= new JSONObject();
            obj.put( "layoutname", f.replaceAll (".txt", "" ) );
            obj.put( "layouthtml", readLayoutFile ( f ) );
            retArr.add(obj);
        }
        obj= new JSONObject();
        obj.put( "isSuccessful", true );
        retArr.add(obj);
    
        System.out.println( retArr.toJSONString() );
        return retArr.toJSONString();
    }