2

using the JSONParser class i've implemented the following code:

String data = '{"Asset__c": "abc123","Opening_Speed__c": "50",  "Closing_Speed__c": "50"}';

    JSONParser parser = JSON.createParser(data);

    parser.nextToken();

    //advance to the next token
    while (parser.nextToken() != NULL) {

        String fieldName = parser.getCurrentName();
        String fieldValue = parser.getText();

        System.debug('Current token is: ' + parser.getCurrentToken() + 
                    ' and FieldName is: ' + fieldName +
                    ' and FieldValue is: ' + fieldValue);


    }

And this returns the following lines in the debug statements. System.Debug output

I can't understand why it's setting the fieldName and the fieldValue variables with the FIELD_NAME the first time through, then setting them as expected (name and value populated correctly) on the second pass...

I've tried adding an IF(parser.getCurrentToken() == 'FIELD_NAME') test in there to separate the setting of fieldName from fieldValue but that throws a type error - cannot compare JSONToken to String... but can't cast that JSONToken to String to do that either..

Obviously, something simple i'm missing here...

4 Answers 4

1

JSONToken is a type of Enum. You can't cast an Enum to a String, but you do have two alternatives.

The preferred alternative is to use the literal Enum token:

if(parser.getCurrentToken() == JSONToken.VALUE_STRING) {
    // code here
}

Otherwise, you could also choose to use name to convert an Enum to a String:

if(parser.getCurrentToken().name() == 'VALUE_STRING') {
    // code here
}

I would strongly advise that you don't do this, because using an Enum token directly avoids the possibility that you'll make a typo in the string.

Also, keep in mind that the token may be one of several types of values, such as VALUE_NULL or VALUE_NUMBER_FLOAT. See the first link in this answer for the possible values you may encounter while parsing JSON.

1
  • Thanks sfdcfox.. I solved this as per below.. .along the lines of the literal Enum token mentioned.. code below Commented Nov 13, 2016 at 5:32
0

I solved this as per below. This now does what i'm looking for :)

String data = '{"Asset__c": "abc123","Opening_Speed__c": "50",  "Closing_Speed__c": "50"}';

    JSONParser parser = JSON.createParser(data);

    String fieldName = '';
    String fieldValue = '';

    parser.nextToken();

    //advance to the next token
    while (parser.nextToken() != JSONToken.END_OBJECT) {


        IF (parser.getCurrentToken() == JSONToken.FIELD_NAME) {

            fieldName = parser.getCurrentName();
            parser.nextToken();

        }

            fieldValue = parser.getText();

        System.debug('Values to insert would be: ' + fieldName + ': ' + fieldValue);

    }

This produces the outputs as per below..

enter image description here

0

Although there are JSONParser and JSONGenerator classes in Apex, most problems can be solved more simply using the JSON Class.

For your example:

String data = '{"Asset__c": "abc123","Opening_Speed__c": "50",  "Closing_Speed__c": "50"}';

Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(data);
for (String key : m.keySet()) {
    Object value = m.get(key);
    System.debug('FieldName is: ' + key + ' and FieldValue is: ' + value);
}
0

Don't loose your time playing with SFDC prehistoric JSON parser, you can use https://github.com/Trigger2991/SFDC-JSON-Parser/blob/master/Util_JSONParser.cls , I tested it and it works like a charm :)

You must log in to answer this question.

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