1

How do I convert the following String to a Person object ?

String content:

String testString = "John|23;Ron|22;Don|32"

Person class:

public class Person{
   private String name;
   private String age;
   //getters and setters for name and age
}

Is there any utility class to convert the testString to Person objects based on the delimiters ";" and "|"

2
  • This look like a job for json.org Commented Feb 28, 2014 at 8:33
  • 2
    @BadSkillz Except for the part where this isn't anything like JSON. Commented Feb 28, 2014 at 8:34

4 Answers 4

4

Not easily; that's not really a standard format. My advice would be to write a static fromString method on Person, and then call Person.fromString() on each item returned by testString.split(";").

Sign up to request clarification or add additional context in comments.

Comments

4

No, you have to write your own parser, because no one could know why to associate the first value to "name" and the second one to "age" - although Reflection could help, but that would be a big overhead for such a task.

For parsing, you could first split at ";" and then for every split array element, split at "|", iterate this array and fill the Person according to the array iteration.

Comments

2

It is not in standard format like json, so you would need to write your own parser. For example, here is the Person class that does what you want:

public class Person {
    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public static Collection<Person> parse(String input) {
        Collection<Person> persons = new HashSet<Person>();
        for (String s : input.split(";")) {
            String[] properties = s.split("\\|");
            persons.add(new Person(properties[0], Integer.parseInt(properties[1])));
        }
        return persons;
    }

    public static void main(String[] args) {
        String testString = "John|23;Ron|22;Don|32";
        System.out.println(parse(testString));

    }

}

If it was in json you could use a json parser.

4 Comments

Good approach, but this is a case where the two operations (splitting the string and converting each piece to an object) really need to be in separate methods, for clarity and testability.
@chrylis sure you can extract a method out of the two splits. My goal was to show an approach.
Does StringUtils split method in apache commons library have better performance compared to String's split method ?
@pad I haven't used StringUtils, but if it uses regex then the performance should be comparable.
1

This might be done in that way.

 public class Person{
      private String name;
      private String age;

        public Person(){}:


       public static List<Person> gerPersons(String personString){
            List<Person> result = new ArrayList<Person>();
            String[] presonsTab = personString.split(";");
            for(String p : personsTab){
              Person person = new Person();
              String personTab = p.split("|");
              if(personTab.length!=2)
                 throw new RuntimeException("Provide proper String");
              person.setName(personTab[0]);
              person.setAge(personTab[1]);
              result.add(person);

          }
          return result;

        }

        public void setName(String n){ this.name = n;}
        public void setAge(String a){this.age = a;}


  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.