Home > Software design >  Java conversion from list to JSONL
Java conversion from list to JSONL

Time:12-22

I'd like to convert a list of objects to JSONL where each object in the list will be a line in the json.

e:g: Let's say I have a list of Person and I want to convert that list to a separate file in the JSONL format

List<Person> personList = Stream.of(
                new Person("John", "Peter", 34),
                new Person("Nick", "young", 75),
                new Person("Jones", "slater", 21 ),
                new Person("Mike", "hudson", 55))
                .collect(Collectors.toList());

Convert List of person to JSONL

{"firstName" : "Mike","lastName" : "harvey","age" : 34}
{"firstName" : "Nick","lastName" : "young","age" : 75}
{"firstName" : "Jack","lastName" : "slater","age" : 21}
{"firstName" : "gary","lastName" : "hudson","age" : 55}

CodePudding user response:

Do you want to turn the object into a string?

CodePudding user response:

enter code here

import com.alibaba.fastjson.JSONArray;

for (int i = 0; i < personList.size(); i ) { JSONObject obj = (JSONObject)array.get(i); System.out.println(obj.toJSONString());enter code here }

  • Related