-1

Java file to set and get the values. I have set the boId as the first:

  if(etsBuildOrder != null){
            buildOrder.setBoId(etsBuildOrder.getBoId());
            buildOrder.setName(etsBuildOrder.getName());
            buildOrder.setFactory(etsBuildOrder.getFactory());
            buildOrder.setStatus(etsBuildOrder.getStatus());
            buildOrder.setIssued(etsBuildOrder.getIssued());
            buildOrder.setTeam(etsBuildOrder.getTeam());
            buildOrder.setType(etsBuildOrder.getType());
            buildOrder.setBuildId(etsBuildOrder.getBuildRequestId());
            buildOrder.setPartNumber(etsBuildOrder.getPartNumber());
            buildOrder.setProductCode(etsBuildOrder.getProductCode());
            buildOrder.setSpecialInstructions(etsBuildOrder.getSpecialInstructions());
            buildOrder.setBoCreationDate(RestWsUtil.convertDateToString(etsBuildOrder.getCreationDate(), Constants.SIMPLE_DATE_FORMAT_DATE_ONLY));
            buildOrder.setBoModifiedDate(RestWsUtil.convertDateToString(etsBuildOrder.getModifiedDate(), Constants.SIMPLE_DATE_FORMAT_DATE_ONLY));
            buildOrder.setChangeHistory(etsBuildOrder.getChangeHistory());

        }

JSON return format. boId is not located at the beginning of JSON:

    {
    "name": "TLO9009",
    "factory": "L-Slider",
    "type": null,
    "boCreationDate": "18 Apr 2018",
    "boModifiedDate": "18 Apr 2018",
    "status": "Pending Approval",
    "team": null,
    "partNumber": null,
    "specialInstructions": "Special Inst",
    "changeHistory": "Pending ApprovalWed Apr 18 10:14:06 SGT 2018",
    "productCode": null,
    "issued": null,
    "multifeature": null,
    "buildId": 0,
    "boId": 141
   }

How the JSON should return. I would like the JSON to return like this:

    {
    "boId": 141
    "name": "TLO9009",
    "factory": "L-Slider",
    "type": null,
    "boCreationDate": "18 Apr 2018",
    "boModifiedDate": "18 Apr 2018",
    "status": "Pending Approval",
    "team": null,
    "partNumber": null,
    "specialInstructions": "Special Inst",
    "changeHistory": "Pending ApprovalWed Apr 18 10:14:06 SGT 2018",
    "productCode": null,
    "issued": null,
    "multifeature": null,
    "buildId": 0
   }

I am new to JAVA and help is much appreciated.Thanks in advance.

Fabian Raj
  • 49
  • 7
  • 5
    Which JSON parser are you using? – Shanu Gupta Apr 23 '18 at 07:20
  • Why do you need to print boId as the first field of the JSON string? – RaffoSorr Apr 23 '18 at 07:21
  • May be you can use ObjectMapper to construct the JSON String – Harshit Apr 23 '18 at 07:22
  • @FabianRaj "GET" is an HTTP-Request method, but not a JSON parser. – Impulse The Fox Apr 23 '18 at 07:23
  • @Raffolox because sometimes, for debugging purposes, it is a lot easier to identify JSON data by the Id. Therefore I always prefer having the Id field on the top of my JSON objects. – Baksteen Apr 23 '18 at 07:31
  • There is limited ordering control that the fasterxml ObjectMapper provides as discussed here https://stackoverflow.com/questions/27577701/jackson-objectmapper-specify-serialization-order-of-object-properties You could possibly try to use `LinkedHashMap` as the structure you serialize, but even with that the serializer will not guarantee you the order to march the order of insertion into the map. You may need to write a customer serializer if the order is of importance. – Oleg Sklyar Apr 23 '18 at 08:13

1 Answers1

2

That is not possible and not necessary. Both JSON objects are identical, there is not order.

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

https://json.org/

  • You are right, that there is no such constraint on the JSON format. But because JSON is nothing else but text it is both possible and may be desired. – Oleg Sklyar Apr 23 '18 at 08:10