我正在尝试使用 GSON 将 ArrayList 转换为 JSON,但数据被截断

Posted

技术标签:

【中文标题】我正在尝试使用 GSON 将 ArrayList 转换为 JSON,但数据被截断【英文标题】:I'm trying to convert ArrayList to JSON with GSON, but data get truncated 【发布时间】:2019-08-22 08:02:54 【问题描述】:

我正在尝试使用 GSON 将 ArrayList 转换为 JSON。它非常适用于小数据。但是当涉及到大型 ArrayList 时,转换后的 JSON 会被截断。 如何将整个 ArrayList 转换为 JSON?

这是我的代码

数组列表如下:

public class ProductCheckList 
    int id;
    String characteristics;
    String specification;
    String total_subpoints;
    String min = null;
    String max = null;
    String key1 = null;
    String spec1 = null;
    String value1 = null;
    String min1 = null;
    String max1 = null;
    String key2 = null, spec2 = null, value2 = null, min2 = null, max2 = null;
    String key3 = null, spec3 = null, value3 = null, min3 = null, max3 = null;
    String key4 = null, spec4 = null, value4 = null, min4 = null, max4 = null;
    String key5 = null, spec5 = null, value5 = null, min5 = null, max5 = null;
    String key6 = null, spec6 = null, value6 = null, min6 = null, max6 = null;
    String specificationValue;
    String audit_id;
    String vin;
    String user_id;
    String vehicle_id;
    String variant_id;

转换为 JSON

ArrayList<ProductCheckList> productCheckLists =new ArrayList<>();
String checklist = new Gson().toJson(productCheckLists);

【问题讨论】:

您能否说明在某些示例数据上“截断”是什么意思? 在 logcat 日志或实际数据中被截断?考虑到我过去对类似问题的经历,我不得不问这个问题。 你应该迭代arrayList,如这个答案***.com/questions/25771822/… @Rohit5k2 在实际数据中得到截断的字符串 【参考方案1】:

我使用了你的代码,只是在 kotlin 中转换(但它是一样的),它工作没有任何问题

   inner class ProductCheckList (
     var id: Int = 0,
     var characteristics: String? = null,
     var specification: String? = null,
     var total_subpoints: String? = null,
     var min: String? = null,
     var max: String? = null,
     var key1: String? = null,
     var spec1: String? = null,
     var value1: String? = null,
     var min1: String? = null,
     var max1: String? = null,
     var key2: String? = null,
     var spec2: String? = null,
     var value2: String? = null,
     var min2: String? = null,
     var max2f: String? = null,
     var spec3: String? = null,
     var min3: String? = null,
     var max3: String? = null,
     var min4: String? = null,
     var max2d: String? = null,
     var key12: String? = null,
     var spec12: String? = null,
     var value12: String? = null,
     var min12: String? = null,
     var max12: String? = null,
     var key2r: String? = null,
     var spec2f: String? = null,
     var value22: String? = null,
     var min245: String? = null,
     var max2455: String? = null,
     var spec355: String? = null,
     var min366: String? = null,
     var max377: String? = null,
     var min3d: String? = null,
     var max3d: String? = null,
     var min4d: String? = null,
     var spec2fdd: String? = null,
     var value22er: String? = null,
     var min245ew: String? = null,
     var max2455w: String? = null,
     var spec355w: String? = null,
     var min366f: String? = null,
     var max377f: String? = null,
     var min3df: String? = null,
     var max3dg: String? = null,
     var min4dg: String? = null
)




 fun check***()
    val productCheckLists = ArrayList<ProductCheckList>()
    productCheckLists.add(ProductCheckList(1,"pippo","pluto","minnie","topolino", "dog","pippo","pluto","minnie","topolino", "dog"
    ,"pippo","pluto","minnie","topolino", "dog","pippo","pluto","minnie","topolino","topolino", "dog","pippo","pluto","minnie","topolino",
        "topolino", "dog","pippo","pluto","minnie","topolino","topolino", "dog","pippo","pluto","minnie","topolino","topolino", "dog",
        "pippo","pluto","minnie","topolino","pluto","minnie","topolino"))
    

`

我注意到它按字母顺序排列元素,因此您可能认为它被截断了。例如元素 min4dg 不是最后一个,所以你可以认为它被截断了

["characteristics":"pippo","id":1,"key1":"pippo","key12":"dog","key2":"pippo","key2r":"topolino","max":"dog","max1":"dog","max12":"topolino","max2455":"minnie","max2455w":"pippo","max2d":"topolino","max2f":"dog","max3":"minnie","max377":"dog","max377f":"topolino","max3d":"pluto","max3dg":"minnie","min":"topolino","min1":"topolino","min12":"minnie","min2":"topolino","min245":"pluto","min245ew":"dog","min3":"pluto","min366":"topolino","min366f":"minnie","min3d":"pippo","min3df":"pluto","min4":"topolino","min4d":"minnie","min4dg":"topolino","spec1":"pluto","spec12":"pippo","spec2":"pluto","spec2f":"dog","spec2fdd":"topolino","spec3":"pippo","spec355":"topolino","spec355w":"pluto","specification":"pluto","total_subpoints":"minnie","value1":"minnie","value12":"pluto","value2":"minnie","value22":"pippo","value22er":"topolino"]

另一种可能性是使用以下代码:

val gson = GsonBuilder().create()
        val arrayValues= gson.toJsonTree(productCheckLists).asJsonArray

【讨论】:

感谢您的回复。当 ArrayList 的值较少时,它可以完美运行。但是当值更多时它会截断数据(比如说 50 个条目) 我刚刚尝试了 60 个字段并且它有效。您是指对象的字段还是数组中的元素数量? 我指的是数组中的元素个数 我刚刚尝试了 65 个元素的数组,每个元素有 60 个字段,它可以工作。我还找到了另一种方法,但结果是一样的。 我用另一种方式编辑了答案来执行json数组

以上是关于我正在尝试使用 GSON 将 ArrayList 转换为 JSON,但数据被截断的主要内容,如果未能解决你的问题,请参考以下文章

与 Gson 的 SharedPreferences 无法正常工作

如果列表包含不同的类,如何使用 gson 将 json 转换为 arraylist?

当从共享prefs获取时,GSON将先前存储的List转换为LinkedTreeMaps的ArrayList

如何防止 Gson 将整数表示为浮点数

是否可以使用 Gson.fromJson() 来获取 ArrayList<ArrayList<String>>?

Konlin中的Gson toJson和ArrayList