Create A List of Integers To Write To The Json String.: Jsonparser Methods

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Reference Apex Standard Classes and Methods

// Create a list of integers to write to the JSON string.


List<integer> intlist = new List<integer>();
intlist.add(1);
intlist.add(2);
intlist.add(3);

// Create an object to write to the JSON string.


A x = new A('X');

// Write data to the JSON string.


gen.writeStartObject();
gen.writeNumberField('abc', 1.21);
gen.writeStringField('def', 'xyz');
gen.writeFieldName('ghi');
gen.writeStartObject();

gen.writeObjectField('aaa', intlist);

gen.writeEndObject();

gen.writeFieldName('Object A');

gen.writeObject(x);

gen.writeEndObject();

// Get the JSON string.


String pretty = gen.getAsString();

System.assertEquals('{\n' +
' "abc" : 1.21,\n' +
' "def" : "xyz",\n' +
' "ghi" : {\n' +
' "aaa" : [ 1, 2, 3 ]\n' +
' },\n' +
' "Object A" : {\n' +
' "str" : "X"\n' +
' }\n' +
'}', pretty);
}
}

JSONParser Methods
Represents a parser for JSON-encoded content.

Usage
Use the System.JSONParser methods to parse a response that's returned from a call to an external service that is in JSON
format, such as a JSON-encoded response of a Web service callout.

Methods
The following are instance methods of the System.JSONParser class.

Method Arguments Return Type Description


clearCurrentToken Void Removes the current token.
After this method is called, a call to hasCurrentToken returns
false and a call to getCurrentToken returns null. You

442
Reference Apex Standard Classes and Methods

Method Arguments Return Type Description


can retrieve the cleared token by calling
getLastClearedToken.

getBlobValue Blob Returns the current token as a BLOB value.


The current token must be of type
JSONToken.VALUE_STRING and must be Base64-encoded.

getBooleanValue Boolean Returns the current token as a Boolean value.


The current token must be of type JSONToken.VALUE_TRUE
or JSONToken.VALUE_FALSE.
The following example parses a sample JSON string and
retrieves a Boolean value.

String JSONContent =
'{"isActive":true}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the Boolean value.
Boolean isActive = parser.getBooleanValue();

getCurrentName String Returns the name associated with the current token.
If the current token is of type JSONToken.FIELD_NAME, this
method returns the same value as getText. If the current token
is a value, this method returns the field name that precedes this
token. For other values such as array values or root-level values,
this method returns null.
The following example parses a sample JSON string. It advances
to the field value and retrieves its corresponding field name.

String JSONContent = '{"firstName":"John"}';


JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the field name for the current value.
String fieldName = parser.getCurrentName();
// Get the textual representation
// of the value.
String fieldValue = parser.getText();

443
Reference Apex Standard Classes and Methods

Method Arguments Return Type Description


getCurrentToken System.JSONToken Returns the token that the parser currently points to or null
if there's no current token.
The following example iterates through all the tokens in a
sample JSON string.

String JSONContent = '{"firstName":"John"}';


JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the next token.
while (parser.nextToken() != null) {
System.debug('Current token: ' +
parser.getCurrentToken());
}

getDatetimeValue Datetime Returns the current token as a date and time value.
The current token must be of type
JSONToken.VALUE_STRING and must represent a Datetime
value in the ISO-8601 format.
The following example parses a sample JSON string and
retrieves a Datetime value.

String JSONContent =
'{"transactionDate":"2011-03-22T13:01:23"}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the transaction date.
Datetime transactionDate =
parser.getDatetimeValue();

getDateValue Date Returns the current token as a date value.


The current token must be of type
JSONToken.VALUE_STRING and must represent a Date value
in the ISO-8601 format.
The following example parses a sample JSON string and
retrieves a Date value.

String JSONContent =
'{"dateOfBirth":"2011-03-22"}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the date of birth.
Date dob = parser.getDateValue();

444
Reference Apex Standard Classes and Methods

Method Arguments Return Type Description


getDecimalValue Decimal Returns the current token as a decimal value.
The current token must be of type
JSONToken.VALUE_NUMBER_FLOAT or
JSONToken.VALUE_NUMBER_INT and is a numerical value
that can be converted to a value of type Decimal.
The following example parses a sample JSON string and
retrieves a Decimal value.

String JSONContent =
'{"GPA":3.8}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the GPA score.
Decimal gpa = parser.getDecimalValue();

getDoubleValue Double Returns the current token as a double value.


The current token must be of type
JSONToken.VALUE_NUMBER_FLOAT and is a numerical value
that can be converted to a value of type Double.
The following example parses a sample JSON string and
retrieves a Double value.

String JSONContent =
'{"GPA":3.8}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the GPA score.
Double gpa = parser.getDoubleValue();

getIdValue ID Returns the current token as an ID value.


The current token must be of type
JSONToken.VALUE_STRING and must be a valid ID.
The following example parses a sample JSON string and
retrieves an ID value.

String JSONContent =
'{"recordId":"001R0000002nO6H"}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();

445
Reference Apex Standard Classes and Methods

Method Arguments Return Type Description


// Get the record ID.
ID recordID = parser.getIdValue();

getIntegerValue Integer Returns the current token as an integer value.


The current token must be of type
JSONToken.VALUE_NUMBER_INT and must represent an
Integer.
The following example parses a sample JSON string and
retrieves an Integer value.

String JSONContent =
'{"recordCount":10}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the record count.
Integer count = parser.getIntegerValue();

getLastClearedToken System.JSONToken Returns the last token that was cleared by the
clearCurrentToken method.

getLongValue Long Returns the current token as a long value.


The current token must be of type
JSONToken.VALUE_NUMBER_INT and is a numerical value
that can be converted to a value of type Long .
The following example parses a sample JSON string and
retrieves a Long value.

String JSONContent =
'{"recordCount":2097531021}';
JSONParser parser =
JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the record count.
Long count = parser.getLongValue();

getText String Returns the textual representation of the current token or null
if there's no current token.
No current token exists, and therefore this method returns null,
if nextToken has not been called yet for the first time or if the
parser has reached the end of the input stream.
For an example, see getCurrentName on page 443.

446

You might also like