Я пытаюсь преобразовать ArrayList в JSON с GSON, но данные усекаются - PullRequest
0 голосов
/ 01 апреля 2019

Я пытаюсь преобразовать ArrayList в JSON, используя GSON.Он отлично работает для небольших данных.Но когда дело доходит до большого 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);

1 Ответ

2 голосов
/ 01 апреля 2019

Я использовал ваш код, только что преобразованный в 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 не последний, так что вы можете подумать, что он перехвачен

[{"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
...