Home > Net >  Convert Delimitted String to Object Structure
Convert Delimitted String to Object Structure

Time:09-22

I have a delimited String: "Albert:GE-20&40#Gert:LE-26#John-$-$#Johnah Doe:LP-40".

Name:Code-Mark1&Mark2&Mark3#

Where, Albert is the name, GE is the code, and 20&40 are marks separated by &. Different entries are separated by #. And if any data is not present then $ is present in its place. Ex. for John, the code and marks are not present so they are replaced by $.

Below is the object structure, I want to emulate:

{
"students":[{
"name": value,
"code":value,
"marks":[{"mark":value}]}]
}

So above example should translate to:

{
"students":[{
"name": "Albert",
"code":"GE",
"marks":[{"mark":"20"},{"mark":"40"}]},
{
"name": "Gert",
"code":"LE",
"marks":[{"mark":"26"}]},
{
"name": "John"
},
{
"name": "Johnah Doe",
"code":"LP",
"marks":[{"mark":"40"}]}
]
}

I was trying to use ConvertString.java to do this, but am stuck. How do I do this? ideally using Java 8 streams/regular expressions/libraries/any other efficient way. Kindly, help.

StudentDetails.java

package com;

import java.util.List;

public class StudentDetails {
    List<Student> students;

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

}

Student.java

package com;

import java.util.List;

public class Student {
    String name;
    String code;
    List<Marks> marks;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<Marks> getMarks() {
        return marks;
    }

    public void setMarks(List<Marks> marks) {
        this.marks = marks;
    }

}

Marks.java

package com;

public class Marks {
    String mark;

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

}

ConvertString.java

package com;

import java.util.ArrayList;
import java.util.List;

public class ConvertString {
    public static void main(String args[])
    {
        String input="Amar:GE-20&40#Gert:LE-26#John-$-$#Johnah Doe:LP-40";
        String newInput="#" input "#";
        List<String> list1 = new ArrayList()<String>(Arrays.asList(newInput.split("#")));

}
}

CodePudding user response:

i think  the simplest way is

 for(list:list1) 
{
        List<String> list2 = new ArrayList()<String>

(Arrays.asList(list.split(":")));

student.getname(list[0]);

List<String> list3 = new ArrayList()<String>

(Arrays.asList(list2[1].split("-")));

student.getcode(list3[0]);

List<String> listmask = new ArrayList()<String>

(Arrays.asList(list3[1].split("&")));

for(mask:listmask)

{
student.masks.add(mask);
}
}
this is my idea hope it can help you 

CodePudding user response:

Using JDK 17:

I have implemented the below code using some of the latest java features apart from Java 8.

Records in java 14 : As of JDK 14, we can replace our data classes with records. Records are immutable classes that require only the type and name of fields. We do not need to create constructor, getters, setters, override toString() methods, override hashcode and equals methods.

The below code will convert input string to Object structure.

public class Test {
  public static void main(String[] args) throws JsonProcessingException {

    String input = "Albert-GE-20&40#Gert-LE-26#John-$-$#John Doe-LP-40#";

    record Marks(String mark){}
    record Student(String name, String code, List<Marks> marks){}
    record Response(List<Student> students){}

    List<Student> students = new ArrayList<>();
    Arrays.asList(input.split("#")).forEach(student -> {

      String[] splitArr1 = student.split("-");
      String[] splitArr2 = splitArr1[2].split("&");

      String name = splitArr1[0];
      String code = splitArr1[1].contains("$")?null:splitArr1[1];
      List<Marks> marks = Arrays.stream(splitArr2).filter(y -> !y.contains("$")).map(Marks::new).toList();

      Student s = new Student(name,code,marks);
      students.add(s);
    });
    Response out = new Response(students);

    System.out.println(out); // Back to Object structure
  }
}

Output::

Response[students=[Student[name=Albert, code=GE, marks=[Marks[mark=20], Marks[mark=40]]], Student[name=Gert, code=LE, marks=[Marks[mark=26]]], Student[name=John, code=null, marks=[]], Student[name=John Doe, code=LP, marks=[Marks[mark=40]]]]]
  • Related