我正在尝试使用GSON将ArrayList转换为JSON。它非常适合小数据。但是当涉及到大型ArrayList时,转换后的JSON将被截断。 如何将整个ArrayList转换为JSON?
这是我的代码
ArrayList如下:
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);
答案 0 :(得分:2)
我使用了您的代码,只是在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 checkStackOverflow(){
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不是最后一个元素,因此您可以认为它是tructted
[{"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