Calculate Json Array Size In Kb or Mb in android?

人盡茶涼 提交于 2020-01-13 11:33:10

问题


I want to calculate json array Size in KB/Mb. I have a condition where I need to calculate json Array size before uploading. It should not be more than 128 KB.


回答1:


Convert your jsonArray to String and use string.getBytes().length . It will give number of bytes used by the string to store the value.

Using those bytes you can calculate the size in any unit.

String.getBytes().length is the number of bytes needed to represent your string in the platform's default encoding. For example, if the default encoding was UTF-16 (rare), it would be exactly 2x the value returned by String.length(). More commonly, your platform encoding will be a multi-byte encoding like UTF-8.




回答2:


JSON is basically an String and encodings determine how much memory is required to store a String. Please read this first.

Now with that knowledge you can simply proceed as follow:

JSON.toString().getBytes(YOUR_PREFERRED_ENCODING).length;



回答3:


you can use the File.length() method to get the file size in bytes.


来源:https://stackoverflow.com/questions/40970411/calculate-json-array-size-in-kb-or-mb-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!