Create A List of Integers To Write To The Json String.: Jsonparser Methods
Create A List of Integers To Write To The Json String.: Jsonparser Methods
Create A List of Integers To Write To The Json String.: Jsonparser Methods
gen.writeObjectField('aaa', intlist);
gen.writeEndObject();
gen.writeFieldName('Object A');
gen.writeObject(x);
gen.writeEndObject();
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.
442
Reference Apex Standard Classes and Methods
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.
443
Reference Apex Standard Classes and Methods
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();
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
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();
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();
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
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.
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