2

I need to convert a String into a list<wrapperObject> or a Map<string, object>

The list contains something like that :

[{"FieldName":"AccountId","FieldType":"lookup-Account","ObjectName":"Opportunity","Operator":"=","Value":"0016E00000TIZUYQA5"}]

this list example contains one row.

and the wrapper class is like that :

public class WrapperLine {
    @AuraEnabled
    public String ObjectName;
    @AuraEnabled
    public String FieldName;
    @AuraEnabled
    public String Operator;
    @AuraEnabled
    public String Value;
    @AuraEnabled
    public String FieldType;
}
8
  • 1
    From where are you getting that String, can you format that String properly, eg Operator == is so confusing, FiedlType has no value, it would be hard to parse such uncertainity Commented Jul 12, 2018 at 9:35
  • 1
    If you have the chance to make it a propper json formatted string, you could just parse it, using something like JSON.deserialize(jsonString, List<WrapperLine>.class)
    – itsmebasti
    Commented Jul 12, 2018 at 9:37
  • FieldType will not be null, it is just in the example, but the Operator cant take the value =, !=, <, >
    – dou
    Commented Jul 12, 2018 at 9:38
  • How can parse a list<String> in lightning component to be a JSON formated string ?
    – dou
    Commented Jul 12, 2018 at 9:38
  • 1
    @dou, now you can apply @Basti's Solution to convert your string in List of wrapper class as : String jsonString = '[{"FieldName":"AccountId","FieldType":"lookup-Account","ObjectName":"Opportunity","Operator":"=","Value":"0016E00000TIZUYQA5"}]'; List<WrapperLine> alist = (List<WrapperLine>)JSON.deserialize(jsonString, List<WrapperLine>.class);
    – sfdc_sk
    Commented Jul 12, 2018 at 10:17

3 Answers 3

1

How about this ?? Sorry to my bad format... first writting..

Public void convert(Object jsonData){
    Wrapper convertedData = (Wrapper)JSON.deserialize(JSON.serialize(jsonData),Wrapper.Class);

 String objectName   = convertedData.getObjectName();
  (do something....)
}

Class Wrapper{
    String objectName;
    String fieldName;
    ( some else...)

    String getObjectName(){
        return objectName;
    }
}
1
  • 3
    Welcome to SFSE! This answer is what many would consider to be a "code dump" (where you provide code, but no explanation for it). Such answers are usually low quality, and have a tendency to generate a lot of "that didn't work" and "how about this?" back-and-forth in the comments. High quality answers tend to explain what the OP's (Original Poster of the question) problem is, why it is a problem, how to fix it, and why the fix works. Answers that follow that general format can end up being helpful to other people with similar (but different) questions too.
    – Derek F
    Commented Jul 12, 2018 at 12:37
2

As long as your string is valid JSON, you can use your Wrapper class, which follows data model of the JSON, and deserialize into it. Simply use JSON.deserialize(<your_json_string>, <wrapper_class>.class); from JSON Class.

String originalValues = '[{"FieldName":"AccountId","FieldType":"lookup-Account","ObjectName":"Opportunity","Operator":"=","Value":"0016E00000TIZUYQA5"}]';

List<WrapperLine> wrapperLines = (List<WrapperLine>)JSON.deserialize(value, List<WrapperLine>.class);
0

I replicated your scenario, Below code gives the required result:

public class manageFiltersDELETE {

public static WrapperLine manageFilters(Map<String, String> lines){
   // System.debug('##--Lines:  '+JSON.deserializeUntyped(lines.get('1')));
     List<Object> reqBody = (List<Object>)JSON.deserializeUntyped(lines.get('1'));
    manageFiltersDELETE manageFiltersDELobj = new manageFiltersDELETE();
    WrapperLine wrapObj  = new WrapperLine((Map<String, Object>)reqBody[0]);
    System.debug('##--wrapObj:  '+wrapObj);
    return wrapObj;


}

public class WrapperLine {
    @AuraEnabled
    public String ObjectName;
    @AuraEnabled
    public String FieldName;
    @AuraEnabled
    public String Operator;
    @AuraEnabled
    public String Value;
    @AuraEnabled
    public String FieldType;

    public WrapperLine(Map<String, Object> lines){
        ObjectName = (String)lines.get('ObjectName');
        FieldName = (String)lines.get('FieldName');
        Operator = (String)lines.get('Operator');
        Value = (String)lines.get('Value');
        FieldType = (String)lines.get('FieldType');

    }
}}

and call above class method like below:

 Map<String, String> mapFilters = new Map<String, String>();

    String filter = '[{"FieldName":"AccountId","FieldType":"lookup-Account","ObjectName":"Opportunity","Operator":"=","Value":"0016E00000TIZUYQA5"}]';

    mapFilters.put('1', filter);

    manageFiltersDELETE.WrapperLine returnBody = manageFiltersDELETE.manageFilters(mapFilters);
1
  • 1
    Again, this is mostly a code dump. You should explain why your solution works to make it a complete answer.
    – Derek F
    Commented Jul 12, 2018 at 12:32

You must log in to answer this question.

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