0% found this document useful (0 votes)
19 views

Json Factory

The document shows how to parse a JSON file using a JSON factory and parser in Java. It creates a JSON factory and parser, checks the first token, then iterates through the array parsing any JSON objects.

Uploaded by

Pratik Kayastha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Json Factory

The document shows how to parse a JSON file using a JSON factory and parser in Java. It creates a JSON factory and parser, checks the first token, then iterates through the array parsing any JSON objects.

Uploaded by

Pratik Kayastha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

JsonFactory factory = new JsonFactory();

JsonParser parser = factory.createJsonParser(new File(yourPathToFile));

JsonToken token = parser.nextToken();


if (token == null) {
// return or throw exception
}

// the first token is supposed to be the start of array '['


if (!JsonToken.START_ARRAY.equals(token)) {
// return or throw exception
}

// iterate through the content of the array


while (true) {

token = parser.nextToken();
if (!JsonToken.START_OBJECT.equals(token)) {
break;
}
if (token == null) {
break;
}

// parse your objects by means of parser.getXxxValue() and/or other parser's


methods

You might also like