Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 426
Nishchal Sharma Avatar asked Dec 05 '16 08:12

Nishchal Sharma


People also ask

How do I find the size of an array in JSON?

To obtain the size of a JSON-encoded array or object, use the json_size function, and specify the column containing the JSON string and the JSONPath expression to the array or object.

How is JSON size calculated?

Use measureLength() to compute the number of characters that will be printed by printTo() . Use measurePrettyLength() to compute the number of characters that will be printed by prettyPrintTo() .

What is the size of a JSON object?

One of the more frequently asked questions about the native JSON data type, is what size can a JSON document be. The short answer is that the maximum size is 1GB.

How many bytes is a JSON?

The maximum length of a JSON type using a binary storage format is 16776192 bytes.


2 Answers

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.

like image 151
Nitesh Avatar answered Oct 24 '22 00:10

Nitesh


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;
like image 45
2hamed Avatar answered Oct 23 '22 23:10

2hamed