0

I'm using spring boot in this case. and Rest controller to return the data.

Here is the JSON i have right now from database using crudrepository :

{
   forms: [
           {
            paent: "",
            posx: 1,
            fieldname: "CostCenter",
            posy: 1,
            ishidden: false,
            level: 0,
            recordname: "CostCenter",
            isrequired: true,
            inputtype: "text",
            issearchable: true,
            label: "Cost Center",
            seq: "1",
            isenabled: false
           },
          {
           parent: "CostCenterDetail",
           ishidden: false,
           level: 1,
           recordname: "CostCenter",
           isrequired: true,
           inputtype: "text",
           issearchable: false,
           label: "Description",
           posx: 1,
           fieldname: "Description",
           posy: 2,
           seq: "5",
           isenabled: true
          },
          {
           parent: "CostCenterDetail",
           ishidden: false,
           level: 1,
           recordname: "CostCenter",
           isrequired: false,
           inputtype: "text",
           issearchable: false,
           label: "Created User",
           posx: 1,
           fieldname: "CreateUserID",
           posy: 4,
           seq: "6",
           isenabled: false
          },
          {
           parent: "CostCenterDetail",
           ishidden: false,
           level: 1,
           recordname: "CostCenter",
           isrequired: false,
           inputtype: "date",
           issearchable: false,
           label: "Created User",
           posx: 2,
           fieldname: "CreateDate",
           posy: 4,
           seq: "7",
           isenabled: false
          },
          {
           parent: "CostCenterDetail",
           ishidden: false,
           level: 1,
           recordname: "CostCenter",
           isrequired: false,
           inputtype: "date",
           issearchable: false,
           label: "Created User",
           posx: 2,
           fieldname: "UpdateDate",
           posy: 5,
           seq: "9",
           isenabled: false
          },
          {
           parent: "CostCenterDetail",
           ishidden: false,
           level: 1,
           recordname: "CostCenter",
           isrequired: true,
           inputtype: "date",
           issearchable: false,
           label: "Effective Date",
           posx: 1,
           fieldname: "Effdt",
           posy: 1,
           seq: "3",
           isenabled: true
          },
          {
           posx: 1,
           fieldname: "CostCenterDetail",
           posy: 2,
           ishidden: false,
           level: 0,
           recordname: "Dummy",
           isrequired: false,
           inputtype: "scroll",
           issearchable: false,
           label: "Cost Center Detail",
           seq: "2",
           isenabled: true
          },
          {
           parent: "CostCenterDetail",
           ishidden: false,
           level: 1,
           recordname: "CostCenter",
           isrequired: false,
           inputtype: "text",
           issearchable: false,
           label: "Created User",
           posx: 1,
           fieldname: "UpdateUserID",
           posy: 5,
           seq: "8",
           isenabled: false
          },
          {
           parent: "CostCenterDetail",
           ishidden: false,
           level: 1,
           recordname: "CostCenter",
           isrequired: true,
           inputtype: "text",
           issearchable: false,
           label: "Status",
           posx: 2,
           fieldname: "Status",
           posy: 1,
           seq: "4",
           isenabled: true
           }
        ]
}

Here is the problem. I want to loop my json on my controller so everytime i find my "level" was changing i need to add this " [] " so my "level:1" data will be inside a new array.

example :

{
  forms : [
            {
              level:0,
              ---data--
                       [ {
                           level : 1,
                           --- data---
                         }
                       ]
             }
          ]
}

here is my rest controller :

@RequestMapping(value = "/alltable" ,method = RequestMethod.GET,  produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(value = HttpStatus.OK)
    public String test() {  
        JSONObject outerObject = new JSONObject();
        outerObject.put("forms", pspnlRepository.findAll());

        return outerObject.toString();
    }

How can i loop through my json and add a new array inside my json ? Any help will be so useful Thanks!

EDIT

Here is my repository code :

public interface pspnlRepository extends CrudRepository<pspnlfield, Long>{


    Iterable<pspnlfield> findAll();
}
Steven tan
  • 139
  • 1
  • 1
  • 12

1 Answers1

0

try this out:

@RequestMapping(value = "/alltable" ,method = RequestMethod.GET,  produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(value = HttpStatus.OK)
    public String test() {  
        JSONObject outerObject = new JSONObject();
        List<Object> pspnlfields=Lists.ArrayList(pspnlRepository.findAll());
        //you can loop here on pspnlfields and add what you want 
        // then put the edited list to your outerObject
        outerObject.put("forms", pspnlfields);

        return outerObject.toString();
    }
Mohamed Nabli
  • 1,629
  • 3
  • 17
  • 24
  • so how can i get key "level" and loop inside. since i try the stackoveflow you send before. it's not match – Steven tan Jun 07 '16 at 10:25
  • that s depend on your object `pspnlField` returned by `pspnlRepository.findAll()`, I just gave you a way to do what you want, instead of looping on json data you loop on the list. – Mohamed Nabli Jun 07 '16 at 10:30
  • so if I use `findAll()` I can't get other key object ? – Steven tan Jun 07 '16 at 10:38