在 android 中计算 Json 数组大小(以 Kb 或 Mb 为单位)?
Calculate Json Array Size In Kb or Mb in android?
我想在 KB/Mb 中计算 json 数组大小。我有一个条件,我需要在上传之前计算 json 数组大小。它不应超过 128 KB。
将您的 jsonArray
转换为 String
并使用 string.getBytes().length
。它将给出字符串用于存储值的字节数。
使用这些字节,您可以计算任何单位的大小。
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 基本上是一个 String
,编码决定了存储 String
需要多少内存。请先read this
现在有了这些知识,您可以简单地进行以下操作:
JSON.toString().getBytes(YOUR_PREFERRED_ENCODING).length;
you can use the File.length() method to get the file size in bytes.
我想在 KB/Mb 中计算 json 数组大小。我有一个条件,我需要在上传之前计算 json 数组大小。它不应超过 128 KB。
将您的 jsonArray
转换为 String
并使用 string.getBytes().length
。它将给出字符串用于存储值的字节数。
使用这些字节,您可以计算任何单位的大小。
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 基本上是一个 String
,编码决定了存储 String
需要多少内存。请先read this
现在有了这些知识,您可以简单地进行以下操作:
JSON.toString().getBytes(YOUR_PREFERRED_ENCODING).length;
you can use the File.length() method to get the file size in bytes.