3

I have below code running fine in developer console.

public class jsonObj{
        public String id;
        public List<jsonObj> children;
    }


void updateTaskMasterMap(Map<String,String> taskMasterIdMap, String parent, List<jsonObj> children)
    {
        for(jsonObj obj: children)
        {
            taskMasterIdMap.put(obj.id, parent);
            if(obj.children != null)
                updateTaskMasterMap(taskMasterIdMap,obj.id,obj.children);
        }
    }

String jsonString = [select Line_Items_Edit_Save_Json_abv__c from Task_List_abv__c where Id='XXXX'].Line_Items_Edit_Save_Json_abv__c ;

system.debug('jsonString@'+jsonString);
Map<String,String> taskMasterIdMap = new Map<String, String>();
String finalJsonString = '{"id":"NoParent","children":' + jsonString + '}';  
       jsonObj topLevel = (jsonObj)JSON.deserializeStrict(finalJsonString, jsonObj.class);
       updateTaskMasterMap(taskMasterIdMap,topLevel.id,topLevel.children);

System.debug('taskMasterIdMap@'+taskMasterIdMap);

It perfectly prints the taskmasterIdMap json. But it is not working when I place it in below class.

public class OTC_TaskList_PrintPage_Draft_Cont {

...//  Put the code here

}

Can someone please let me know where I do wrong.

I face a compile error on below line.

Error: Compile Error: Expecting ')' but was: 'jsonString@' at line

system.debug('jsonString@'+jsonString);

1 Answer 1

4

You are trying to write some code outside a method which is not allowed in apex class, following would work:

public class OTC_TaskList_PrintPage_Draft_Cont {

    public class jsonObj{
        public String id;
        public List<jsonObj> children;
    }

    void updateTaskMasterMap(Map<String,String> taskMasterIdMap, 
        String parent, 
        List<jsonObj> children) {

        for(jsonObj obj: children) {
            taskMasterIdMap.put(obj.id, parent);
            if(obj.children != null)
                updateTaskMasterMap(taskMasterIdMap,obj.id,obj.children);
        }

        // moving following code inside of method
        String jsonString = [select Line_Items_Edit_Save_Json_abv__c 
            from Task_List_abv__c where 
            Id='XXXX'].Line_Items_Edit_Save_Json_abv__c ;

        system.debug('jsonString@'+jsonString);
        Map<String,String> taskMasterIdMap = new Map<String, String>();
        String finalJsonString = '{"id":"NoParent","children":' + jsonString + '}';  
        jsonObj topLevel = (jsonObj) JSON.deserializeStrict(finalJsonString, 
            jsonObj.class);
        updateTaskMasterMap(taskMasterIdMap,topLevel.id,topLevel.children);

        System.debug('taskMasterIdMap@'+taskMasterIdMap);
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .