How to parse a JSON Input stream

前端 未结 11 1911
野的像风
野的像风 2020-11-30 01:06

I am using java to call a url that returns a JSON object:

url = new URL(\"my URl\");
urlInputStream = url.openConnection().getInputStream();
<
11条回答
  •  情深已故
    2020-11-30 01:35

    All the current answers assume that it is okay to pull the entire JSON into memory where the advantage of an InputStream is that you can read the input little by little. If you would like to avoid reading the entire Json file at once then I would suggest using the Jackson library (which is my personal favorite but I'm sure others like Gson have similar functions).

    With Jackson you can use a JsonParser to read one section at a time. Below is an example of code I wrote that wraps the reading of an Array of JsonObjects in an Iterator. If you just want to see an example of Jackson, look at the initJsonParser, initFirstElement, and initNextObject methods.

    public class JsonObjectIterator implements Iterator>, Closeable {
        private static final Logger LOG = LoggerFactory.getLogger(JsonObjectIterator.class);
    
        private final InputStream inputStream;
        private JsonParser jsonParser;
        private boolean isInitialized;
    
        private Map nextObject;
    
        public JsonObjectIterator(final InputStream inputStream) {
            this.inputStream = inputStream;
            this.isInitialized = false;
            this.nextObject = null;
        }
    
        private void init() {
            this.initJsonParser();
            this.initFirstElement();
            this.isInitialized = true;
        }
    
        private void initJsonParser() {
            final ObjectMapper objectMapper = new ObjectMapper();
            final JsonFactory jsonFactory = objectMapper.getFactory();
    
            try {
                this.jsonParser = jsonFactory.createParser(inputStream);
            } catch (final IOException e) {
                LOG.error("There was a problem setting up the JsonParser: " + e.getMessage(), e);
                throw new RuntimeException("There was a problem setting up the JsonParser: " + e.getMessage(), e);
            }
        }
    
        private void initFirstElement() {
            try {
                // Check that the first element is the start of an array
                final JsonToken arrayStartToken = this.jsonParser.nextToken();
                if (arrayStartToken != JsonToken.START_ARRAY) {
                    throw new IllegalStateException("The first element of the Json structure was expected to be a start array token, but it was: " + arrayStartToken);
                }
    
                // Initialize the first object
                this.initNextObject();
            } catch (final Exception e) {
                LOG.error("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e);
                throw new RuntimeException("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e);
            }
    
        }
    
        private void initNextObject() {
            try {
                final JsonToken nextToken = this.jsonParser.nextToken();
    
                // Check for the end of the array which will mean we're done
                if (nextToken == JsonToken.END_ARRAY) {
                    this.nextObject = null;
                    return;
                }
    
                // Make sure the next token is the start of an object
                if (nextToken != JsonToken.START_OBJECT) {
                    throw new IllegalStateException("The next token of Json structure was expected to be a start object token, but it was: " + nextToken);
                }
    
                // Get the next product and make sure it's not null
                this.nextObject = this.jsonParser.readValueAs(new TypeReference>() { });
                if (this.nextObject == null) {
                    throw new IllegalStateException("The next parsed object of the Json structure was null");
                }
            } catch (final Exception e) {
                LOG.error("There was a problem initializing the next Object: " + e.getMessage(), e);
                throw new RuntimeException("There was a problem initializing the next Object: " + e.getMessage(), e);
            }
        }
    
        @Override
        public boolean hasNext() {
            if (!this.isInitialized) {
                this.init();
            }
    
            return this.nextObject != null;
        }
    
        @Override
        public Map next() {
            // This method will return the current object and initialize the next object so hasNext will always have knowledge of the current state
    
            // Makes sure we're initialized first
            if (!this.isInitialized) {
                this.init();
            }
    
            // Store the current next object for return
            final Map currentNextObject = this.nextObject;
    
            // Initialize the next object
            this.initNextObject();
    
            return currentNextObject;
        }
    
        @Override
        public void close() throws IOException {
            IOUtils.closeQuietly(this.jsonParser);
            IOUtils.closeQuietly(this.inputStream);
        }
    
    }
    

    If you don't care about memory usage, then it would certainly be easier to read the entire file and parse it as one big Json as mentioned in other answers.

提交回复
热议问题