Most of your variables are superfluous, and you could clean it up so it'd be easier to read.
The main problem is that you're never breaking out of your innermost loop, and so the same lead record keeps getting used over and over again. You need to test for the END_OBJECT, and when you get there, add the record to the list and then start over again. The innermost loop should read while(parser.nextToken() != JSONToken.END_OBJECT)
, and the one outside that should technically be while(parser.nextToken() != JSONToken.END_ARRAY)
. However, using a simple state machine, which I've included here, you can eliminate most of the complexity, along with a map that translates field names into values.
Here's a simple version of how I'd accomplish the same task:
Map<String, SobjectField> fieldMap = new Map<String, SObjectField> {
'first name' => Lead.FirstName,
'last name' => Lead.LastName,
'email' => Lead.Email // keep going!
};
Lead tempLead;
Lead[] leadList = new Lead[0];
String fieldName;
while(parser.nextToken() != null) {
// Save field name, next round we'll get the value
if(parser.getCurrentToken() == JsonToken.FIELD_NAME) {
fieldName = parser.getText();
continue;
}
// Special case-- if the field name isn't null, we know a value is here
if(fieldName != null && fieldMap.containsKey(fieldName)) {
tempLead.put(fieldMap.get(fieldName), parser.getText());
fieldName = null;
continue;
}
// We check for a new object later, because it's a rarer event.
if(parser.getCurrentToken() == JsonToken.START_OBJECT) {
tempLead = new Lead();
continue;
}
// Once we know end of object, we can start a new record.
if(parser.getCurrentToken() == JsonToken.END_OBJECT) {
leadList.add(tempLead);
tempLead = null;
continue;
}
}
This isn't the only right answer, but hopefully it'll get you where you want to go.