在Android中以Kb或Mb计算Json数组大小? [英] Calculate Json Array Size In Kb or Mb in android?

查看:169
本文介绍了在Android中以Kb或Mb计算Json数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算KB/Mb中的json数组大小.我有一个条件,我需要在上传之前计算json数组的大小.它不能超过128 KB.

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.

推荐答案

将您的jsonArray转换为String并使用string.getBytes().length.它将给出字符串用来存储值的字节数.

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是表示所需的字节数 您的字符串采用平台的默认编码.例如,如果 默认编码为UTF-16(稀有),它恰好是值的2倍 由String.length()返回.更常见的是,您的平台编码 将是类似于UTF-8的多字节编码.

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.

这篇关于在Android中以Kb或Mb计算Json数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆