First of all, you didn’t post the specific error message. As for OOM, for extremely large (although your 1.3M is not called extremely large) json files, usingJson Stringis bound to OOM. It is recommended to use FastJson, which has a built-in API for processing large json files https://github.com/alibaba/fastjson/wiki/Stream-api
JSONReader reader = new JSONReader(new FileReader("/tmp/huge.json")); reader.startArray(); while(reader.hasNext()) { VO vo = reader.readObject(VO.class); // handle vo ... } reader.endArray(); reader.close();
JSONReader reader = new JSONReader(new FileReader("/tmp/huge.json")); reader.startObject(); while(reader.hasNext()) { String key = reader.readString(); VO vo = reader.readObject(VO.class); // handle vo ... } reader.endObject(); reader.close();
Gson assigns values to UserBean in one-to-one correspondence by searching for key. . . . So, you must ensure that the Key in the Json data returned in the API is the same as the UserBean.
First of all, you didn’t post the specific error message.
As for OOM, for extremely large (although your 1.3M is not called extremely large) json files, using
Json String
is bound to OOM.It is recommended to use FastJson, which has a built-in API for processing large json files
https://github.com/alibaba/fastjson/wiki/Stream-api
Consider using stream operations for large files, and try not to load the entire string directly. Gson provides JsonReader for file stream operations.
Gson provides streams, just don’t use String.
Let me post how to report the error first.
Try using other open source libraries~ If not, parse jsonObject/JsonArray yourself
I was the fool this time. . .
Gson assigns values to UserBean in one-to-one correspondence by searching for key. . . .
So, you must ensure that the Key in the Json data returned in the API is the same as the UserBean.