简体   繁体   中英

Calculate Json Array Size In Kb or Mb in android?

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.

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.

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;
you can use the File.length() method to get the file size in bytes.

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