[SalesForce] Convert JSON String to a list

How do i parse a JSON String to a list.

Here,
I have a class named LatestStatus and it has its variables. I need to expose these variable as table in lightning component.

JSON String :

    {
  "@xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
  "shipmentMilestones": {
    "shipmentsTracked": {
      "shipmentPrefix": "081",
      "masterDocumentNumber": "26558954",
      "origin": "GLADSTONE AIRPORT GLADSTONE",
      "destination": "SYDNEY",
      "consigneeName": "SYDNEY FISH MARKET",
      "shipperName": "CHRISTOPHER PUTMAN",
      "shipmentDescription": "LIVE CRABS",
      "handlingCode": "AVP",
      "pieces": "4",
      "weight": "64.0",
      "weightUnit": "Kg",
      "units": null,
      "latestStatus": {
        "station": "SYD",
        "milestone": "Delivered",
        "shipmentdate": "25-Jun-2017 21:22:00",
        "weight": "64.0",
        "pieces": "4",
        "flightDetails": null
      },
      "history": [
        {
          "station": "SYD",
          "milestone": "Delivered",
          "shipmentdate": "25-Jun-2017 21:22:00",
          "weight": "64.0",
          "pieces": "4",
          "flightDetails": null
        },
        {
          "station": "SYD",
          "milestone": "Checked In",
          "shipmentdate": "25-Jun-2017 16:24:38",
          "weight": "7.0",
          "pieces": "1",
          "flightDetails": "QF0533, ARR ; SYD. 25-Jun-2017 15:37(A)"
        },
        {
          "station": "SYD",
          "milestone": "Checked In",
          "shipmentdate": "25-Jun-2017 16:24:38",
          "weight": "64.0",
          "pieces": "4",
          "flightDetails": "QF0533, ARR ; SYD. 25-Jun-2017 15:37(A)"
        },
        {
          "station": "SYD",
          "milestone": "Checked In",
          "shipmentdate": "25-Jun-2017 16:24:38",
          "weight": "69.0",
          "pieces": "4",
          "flightDetails": "QF0533, ARR ; SYD. 25-Jun-2017 15:37(A)"
        },
        {
          "station": "GLT",
          "milestone": "Accepted",
          "shipmentdate": "25-Jun-2017 09:18:34",
          "weight": "64.0",
          "pieces": "4",
          "flightDetails": null
        }
      ]
    }
  }
}

Response Class :

public class Freight_AWBResponseParsing {

    public class LatestStatus {
        public String station;
        public String milestone;
        public String creationdate;
        public String weight;
        public String pieces;
        public string flightDetails;
        public String unit;
    }

    public String xmlnssoapenv;
    public ShipmentMilestones shipmentMilestones;


    public class ShipmentMilestones {
        public ShipmentsTracked shipmentsTracked;
    }

    public class ShipmentsTracked {
        public String shipmentPrefix;
        public String masterDocumentNumber;
        public String origin;
        public String destination;
        public String consigneeName;
        public String shipperName;
        public String shipmentDescription;
        public String handlingCode;
        public String pieces;
        public String weight;
        public String weightUnit;
        public string units;
        public LatestStatus latestStatus;
        public List<LatestStatus> history;
    }


    public static Freight_AWBResponseParsing parse(String json) {
        return (Freight_AWBResponseParsing) System.JSON.deserialize(json, Freight_AWBResponseParsing.class);
    }
}

UPDATED

Controller:

In my controller am parsing the JSON String like this, and trying to achieve this :

            Freight_AWBResponseParsing awbResponseParsed = Freight_AWBResponseParsing.parse(res.getBody());
        obj.lsList = awbResponseParsed.ShipmentMilestones.shipmentsTracked.history;
        system.debug(' @@ Latest Status List @@ ' + obj.lsList);

public class WrapperClass{

        @AuraEnabled
        public list<Freight_AWBResponseParsing.LatestStatus> lsList {get;set;}

    }

Component :

<aura:attribute name="TableWrapper" type="Freight_tableDisplayClass.WrapperClass"/>

<aura:iteration items="{!v.TableWrapper.lsList}" var="wrap">
        <ui:outputtext value="{!wrap.station}"/>
</aura:iteration>

Component is not displaying the list of wrapper.

In Debug logs :
I get this, How do i convert this to list <LatestStatus> ??

@@ Latest Status List @@ (LatestStatus:[creationdate=null, flightDetails=null, milestone=Delivered, pieces=4, station=SYD, unit=null, weight=64.0], LatestStatus:[creationdate=null, flightDetails=QF0533, ARR ; SYD. 25-Jun-2017 15:37(A), milestone=Checked In, pieces=1, station=SYD, unit=null, weight=7.0], LatestStatus:[creationdate=null, flightDetails=QF0533, ARR ; SYD. 25-Jun-2017 15:37(A), milestone=Checked In, pieces=4, station=SYD, unit=null, weight=64.0], LatestStatus:[creationdate=null, flightDetails=QF0533, ARR ; SYD. 25-Jun-2017 15:37(A), milestone=Checked In, pieces=4, station=SYD, unit=null, weight=69.0], LatestStatus:[creationdate=null, flightDetails=QF2335,DEP ; GLT.25-Jun-2017 10:31(A) ARR ; BNE. 25-Jun-2017 11:30(E), milestone=Departed, pieces=4, station=GLT, unit=null, weight=64.0], …)

Best Answer

You have two options. Both involve visibility enabled by the @AuraEnabled annotation.

Option 1:

Annotate all the properties of the inner LatestStatus class. This will enable Lightning to see them.

Eg:

public class LatestStatus {
    @AuraEnabled
    public String station;
    @AuraEnabled
    public String milestone;
    //etc...

Option 2:

Serialize the LatestStatus list in Apex and deserialize as an Array of generic objects in lightning (no wrapper class needed)

public list<Freight_AWBResponseParsing.LatestStatus> lsList;
lsList = awbResponseParsed.ShipmentMilestones.shipmentsTracked.history;
lsListString = JSON.serialize(lsList);
return listListString

In the JS component:

<aura:attribute name="lsList" type="Object[]" />

In the JS controller/helper action callback:

var lsList = JSON.parse(response.getReturnValue());
component.set("v.lsList",lsList);
//apologies if any syntax is slightly wrong here - this is untested

Now you can iterate on the lslist.

BTW, option 2 is faster - currently typed objects passed from Apex to the front end lightning components are much slower that using untyped serialized JSON strings.

Related Topic