0

i have succesfully parse my JSON data. but now i want data back from following APEX method "getCustomerData()" like below:

DxModel[id=2, [email protected], first_name=clerk, last_name=dx, BillingAddress[city=Chicago, region=IL, postcode=60622, phone=0000000000]]

here is my model class:

global with sharing class DxModel {

    /* inner Classes' Instances */
    public BillingAddress billing_address {get;set;}
    public List<Addresses> addresses {get;set;}
    public String id {get;set;}
    public String email {get;set;}
    public String first_name {get;set;}
    public String last_name {get;set;}

    /* inner class */
    global with sharing class BillingAddress{

        public String city {get;set;}
        public String state {get;set;}
        public String postcode {get;set;}
        public String phone {get;set;}
    }

    global with sharing class Addresses{

        public String city {get;set;}
        public Region region {get;set;}
        public String postcode {get;set;}
        public String telephone {get;set;}              
    }

    global with sharing class Region{

        public String region_code {get;set;}
    }
}

is there any way to assign value from "Region" class and from "Addresses" class to "BillingAddress" class so i can get above output

here is my JSON data:

{"id":2,"created_at":"2016-06-30 09:59:12","updated_at":"2016-06-30 11:11:56","created_in":"Default Store View","email":"[email protected]","firstname":"clerk","lastname":"dx","gender":1,"store_id":1,"website_id":1,"addresses":[{"region":{"region_code":"IL"},"street":["123 main street","1050 Marshfield Ave #3"],"company":"Jhon DX","telephone":"0000000000","postcode":"60622","city":"Chicago","firstname":"clerk","lastname":"dx","default_billing":true}

This is my APEX method:

public List<DxModel> getCustomerData(){

    parser = JSON.createParser("My JSON DATA");                   
    List<DxModel> customerList = new List<DxModel>();        
    while (parser.nextToken() != null) {
        if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
            while(parser.nextToken() != null)  {
                if(parser.getCurrentToken() == JSONToken.START_OBJECT) {                
                    DxModel customer = (DxModel)parser.readValueAs(DxModel.class);
                    customerList.add(customer);
                }
            }
        }
    }               
    return customerList;
}
1
  • since your data model allows for multiple Address how would you determine which Address has the Region you want for the BillingAddress?
    – cropredy
    Commented Jul 5, 2016 at 18:41

1 Answer 1

0

Once you have the apex Object ,you can treat like a normal object and extract values via simple object.properties

List<DxModel> lstDxModels = getCustomerData();
for(DxModel dxmodel:lstDxModels ){
   system.debug(dxmodel);
   system.debug(dxmodel.addresses); 
   for(Addresses add :dxmodel.addresses){
      system.debug(add.Region.region_code);
   }
   //Your BillingAddress is a single object while your apex object returns an error 
   //you can use dxmodel.addresses[0] to get first element if list has only one element
}
1
  • hi Mohth, thanks for your valuable comment. actually this is not my problem. because of my "getCustomerData()" method provide the parsed data to another APEX class which need data in exact form and that class used by other many classes so i can't modify that class. so i need exact following format: DxModel[id=2, [email protected], first_name=clerk, last_name=dx, BillingAddress[city=Chicago, region=IL, postcode=60622, phone=0000000000]]
    – Rajinder
    Commented Jul 5, 2016 at 4:37

You must log in to answer this question.

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