+ Start a Discussion
DahveedDahveed 

Putting parsed JSON into a list and accessing individual fields in that list

I'm a newb and trying to do something I know should be basic but can't seem to find the answer anywhere. I'm using a webcallout to grab new leads created in a separate system which returns it's result in JSON. I've parse it using the following code. My idea was to put each object in the JSON into a list and then use the person_id(will write this to any new lead I create through the webcallout) field  to search my current leads for duplicates before I do an insert. However in trying to see if I'm getting the proper information I've realized I can't figure out how to access individual fields in the list as I am used to doing it with an Sobject.  Please see the code below.

-----------

List<Object> dcObjectList = new List<Object>();

if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'object')){
parser.nextToken();
donationCreated dc = (donationCreated)parser.readValueAs(donationCreated.class);
dcObjectList.add(dc);
}

for(integer i=0; i <dcObjectList.size();i++){
system.debug('Person ID: ' + dcObjectList.get(i));

// This returns donationCreated:[amount_in_cents=100, city=Rivside, country=US, created_at=2012-11-02 08:12:11 UTC, //donation_type=cc, email=scott@sc.com, first_name=Scott, id=1926, last_name=Moe, person_id=37911, //phone_number=95225742xx, state=CA, status=processed, street_address2=null, street_address=xxxx Oxd St, //transaction_id=ch_0ezXsb, zip_code=xxx04]
}

public class donationCreated {
public string id;
public string amount_in_cents;
public string person_id;
public string email;
public string first_name;
public string last_name;
public string street_address;
public string street_address2;
public string city;
public string state;
public string zip_code;
public string country;
public string phone_number;
public string transaction_id;
public string donation_type;
public string status;
public string created_at;

}

 

When I try to use dcObjectList.get(i).email I get 

Compile Error: Initial term of field expression must be a concrete SObject:

 

 

Please help

 

Dahveed

Best Answer chosen by Admin (Salesforce Developers) 
DahveedDahveed

My lack of programming background.  My list needed to be of  type the class I had created.

 

List<donationCreated> dcObjectList = new List<donationCreated>

 

then I can iterate back over and use dot notation.

All Answers

kiranmutturukiranmutturu

for sobjects only you can get the info usign get method..but not with custom apex class instance .....

DahveedDahveed

Thanks Kiran_mutturu, so what should I be using to get the individual fields if not get()?

kiranmutturukiranmutturu

you need the create the instance to donationCreated and use '.' notation to get the value......

DahveedDahveed

It is instantiated already with donationCreated dc = (donationCreated)parser.readValueAs(donationCreated.class);

 

I can access it using dot notation within the instance such as dc.person_id. However there will be multiple instances created as it iterates over the JSON.  So I'm adding each instance to a list so I can bulk search for duplicates instead of running indv SOQL searches. My problem is when I iterate back over the list I don't know what to use to access individual fields rather than the whole list.

Ex: 

donationCreated dc = (donationCreated)parser.readValueAs(donationCreated.class); // This instantiates the donationCreated class

This calls the donationCreated class and makes the JSON into accessible string variables which I can access using dc.person_id or dc.email

I want to add all the fields into a list each time it iterates over donationCreated dc = (donationCreated)parser.readValueAs(donationCreated.class);

So I use dcObjectList.add(dc);

 

When I go back and iterate over the dcObjectList using the dcObjectList.get(i)  it works but it returns the List. Rather I want to access the individual fields in that list instance.  In an Sobject I would just use dot notation but it doesn't work here. What do I use instead of dcObjectList.get(i)  ??  

DahveedDahveed

My lack of programming background.  My list needed to be of  type the class I had created.

 

List<donationCreated> dcObjectList = new List<donationCreated>

 

then I can iterate back over and use dot notation.

This was selected as the best answer
Santosh SSantosh S

Using 'Mass Email Lead' option for sending Email to particular Lead

 

Need help to track the click on the link (inside the email) by the Email receiver (only the count of Click on the link to be tracked)

 

 

Please Help

 

Warm Regards