0
    public class test{
        public  Boolean one{ get; set; }
        public  Boolean two{ get; set; }
        public  Boolean three{ get; set; }
        public  Boolean four{ get; set; }
        public  Boolean five{ get; set; }
    }

    public class test1{
        public String TOne{ get; set; }
        public test TTwo{ get; set; }
    }

Expected JSON by serializing

{ "TOne": "First",  "TTwo": {"one": true,two": true, "three": true,"four": true,"five": false}},{ "TOne": "Second",  "TTwo": {"one": true,"two": true,"three": true,"four": true, "five": false}}}

 public static string test(List<String> NameList){
    try{
        TTwo Options = new TTwo ();
        Data  wrapper = new Data ();
        String wrpper;
        if (NameList != null) {
            for(String Name:  NameList{
        //I need to assign 
        wrapper.TOne = Name; 

            }

        }
        //Get custom setting and pass value to wrapper
        xxx Customsetting= [xxxx][0];
       Options.xxxx = Customsetting.xxxxx;
       Options.xxx = Customsetting.xxxxx;
       Options.xxx = Customsetting.xxxxx;
       Options.xxx = Customsetting.xxxxx;
       Options.xxx = Customsetting.xxxxx;

        wrapper.TTwo = Options;
        String reqBody = JSON.serialize(wrapper); ****//Here Issue is only get one array**(*{ "TOne": "First",  "TTwo": {"one": true,two": true, "three": true,"four": true,"five": false}*})**


    //Do the HTTP Callout here
    }
    catch(CalloutException exp){
        return exp.getMessage();
    }

public class Data {
    public String TOne;
    public TTwo TTwo;
}

public class TTwo {
    public Boolean one;
    public Boolean two;
    public Boolean three;
    public Boolean four;
    public Boolean five;
}

How can get a Expected JSON with multiple arrays(It's in above), now only result one, Where is the issue? Can Someone help me?

2
  • Your Expected JSON is invalid JSON format, also your question is not clear. Please edit. Dec 1, 2023 at 9:27
  • @RahulGawale Edited it,Hope now it's better
    – SFL
    Dec 1, 2023 at 11:00

1 Answer 1

1

Your JSON is invalid. You can use the JSON Viewer or JSONLint to validate the JSON.

Suppose your JSON is like this:

{"data":[{"TOne":"First","TTwo":{"one":true,"two":true,"three":true,"four":true,"five":false}},{"TOne":"Second","TTwo":{"one":true,"two":true,"three":true,"four":true,"five":false}}]}

Then your wrapper class look like as below. Wrapper Class:

public List<Data> data;

    public class Data {
        public String TOne;
        public TTwo TTwo;
    }

    public class TTwo {
        public Boolean one;
        public Boolean two;
        public Boolean three;
        public Boolean four;
        public Boolean five;
    }

You can JSON2Apex to convert your JSON to apex class.

=========================================================

Updated Code:

public static string test(List<String> NameList){
    try{

        List<Data> listOfData = new List<Data>();        
        
        //Get custom metadata and pass value to wrapper
        xxx Customsetting= [xxxx][0];

        if (NameList.size() > 0) {
            for(String Name :  NameList{
                Data wrapper = new Data ();
                //I need to assign 
                wrapper.TOne = Name; 
                TTwo Options = new TTwo ();
                Options.one = Customsetting.xxxxx;
                Options.two = Customsetting.xxxxx;
                Options.three = Customsetting.xxxxx;
                Options.four = Customsetting.xxxxx;
                Options.five = Customsetting.xxxxx;
                wrapper.TTwo = Options;
                listOfData.add(wrapper);
            }
        }
        
        String reqBody = JSON.serialize(listOfData); 

    //Do the HTTP Callout here
    } catch(CalloutException exp){
        return exp.getMessage();
    }
}

public class Data {
    public String TOne;
    public TTwo TTwo;
}

public class TTwo {
    public Boolean one;
    public Boolean two;
    public Boolean three;
    public Boolean four;
    public Boolean five;
}
6
  • I have a doubt how I can come up with it with my apex above
    – SFL
    Dec 1, 2023 at 11:02
  • @SFL, Please share the full apex class which you have tried. Dec 1, 2023 at 11:05
  • It's there in my edited
    – SFL
    Dec 1, 2023 at 11:08
  • @SFL check in my answer i have added the updated code. Dec 1, 2023 at 11:14
  • Thanks.Let me try and update you.
    – SFL
    Dec 1, 2023 at 11:54

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