1

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], ...)

  • Isn't history already a List collection? What is obj.lsLst? – David Cheng Aug 19 '17 at 2:06
  • It is a List<LatestStatus> - what are you trying to do with it - I'm guessing you are passing it back to the lightning component? If so, then if you want to see the values in lightning, you need to declare any attribute that you want to access as @AuraEnabled – Caspar Harmer Aug 19 '17 at 4:46
  • Also, I'm not sure how lightning handles multiply nested inner classes - perhaps it might be better to declare the LatestStatus class outside of the parent Freight_AWBResponseParsing class – Caspar Harmer Aug 19 '17 at 4:48
  • with the List<LatestStatus> am trying to pass it to lightning component as a <Aura:Iteration> list and render its variable values in the table. I declare list<Freight_AWBResponseParsing.LatestStatus> lsList {get;set;} as a Wrapper and try to store lsList with List<LatestStatus> values. When i call that inAura:Iteration its not returning any values. – AjaySFDC Aug 19 '17 at 11:58
  • @CasparHarmer : I have updated my question. I want the list<LatestStatus> to be displayed as table in the lightning component. – AjaySFDC Aug 19 '17 at 12:17
1

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.

  • First option did not work.. But the second option worked fine. Thanks Caspar. – AjaySFDC Aug 19 '17 at 21:33
0

I tried a 3rd option and it worked as well

I added the variables in the LatestStatus class on the base class i had. Example below , and used the list of base class as return type for my component's iteration list.

global class Freight_tableDisplayClass {

    @AuraEnabled public String station {get;set;}
    @AuraEnabled public String weight {get;set;}

    @AuraEnabled
    public static list<Freight_tableDisplayClass> getWrapperlist(Id CaseId){

        list<Freight_tableDisplayClass> allTableData = new list<Freight_tableDisplayClass>();

        Freight_AWBResponseParsing awbResponseParsed = Freight_AWBResponseParsing.parse(res.getBody());        
        for(Freight_AWBResponseParsing.LatestStatus ls : awbResponseParsed.ShipmentMilestones.shipmentsTracked.history){
            Freight_tableDisplayClass obj = new Freight_tableDisplayClass();
            obj.station = ls.station;
            obj.weight = ls.weight;
            allTableData.add(obj);
        }        

        system.debug(' @@ Latest Status List @@ ' + allTableData);
        //system.debug(' @@ wr Status List @@ ' + obj.aRow);

        return allTableData;

    }
}

Component :

    <aura:attribute name="ApexController" type="Freight_tableDisplayClass[]"/>

<aura:iteration items="{!v.ApexController}" var="wrap">
    <ui:outputtext value="{!wrap.weight}"/>    
</aura:iteration>

js controller :

component.set("v.ApexController", response.getReturnValue());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.