Saurabh Borse
Saurabh Borse

Reputation: 163

I'm trying to convert ArrayList to JSON with GSON, but data get truncated

I'm trying to convert ArrayList to JSON using GSON. It works perfectly for small data. But when it comes to large ArrayList the converted JSON get truncated. How can I convert entire ArrayList to JSON?

Here is my code

ArrayList is as below:

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;
}

Conversion to JSON

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

Upvotes: 0

Views: 458

Answers (1)

Peppe Scab
Peppe Scab

Reputation: 150

I used your code , just converted in kotlin (but it's the same) and it worked without any problem

   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"))
    }

`

What I've noticed is that it puts elements in alphabetical order so maybe for that you think that is truncated. For example element min4dg is not the last one so you could think it is trucanted

[{"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"}]

The other possibility is using the following code:

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

Upvotes: 2

Related Questions