问题
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