简体   繁体   中英

How to convert a large JSON string to JSON Object?

My spring application does a rest request to a server and the response from the server is a JSONObject string. The JSON string is very huge(200MB). I want to convert the json string to a JSONObject. Below are my code for conversion:

exchange = restTemplate.exchange(Url, HttpMethod.POST, postEntity, String.class);
jsonObject = objectMapper.readValue(exchange.getBody(), JSONObject.class);

For a single request, it is taking 3-5 seconds for conversion. But, if there are multiple requests the conversion is taking so much time (60 seconds for 8-10 requests in parallel). Is there any better way to do this?

I'd say that transforming a 200MB chunk of JSON to an object using jackson-databind's ObjectMapper will almost always consume a lot of computing time and furthermore huge amounts of memory.

If you don't need the whole object represented by the JSON in memory at a single time, ie chunks of it will suffice, I would advise to switch to an approach that utilizes jackson's streaming API . You can combine it with databind on smaller subsets of the JSON, passing the resulting DTOs to some consumer (kind of visitor pattern).

I hope this is of any help for your special use-case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM