0

I have a text file that contains letters and numbers. This letter-number combination holds information about a user. For example: "P555636477JohnDoeEA 55687878 9789899 2899298" First letter can be either "P/G/S" meaning Premium, Gold or Simple. Follows his telephone number and name. Two letters E and A mean native language(English) and nationality (American). Then follows his other telephone numbers, that he has registered. There might be more than 3 numbers. My question is: what is the best method to parse this information into JSON object? Im thinking about using regex and StringBuilder, but Im not sure how to approach those telephone numbers at the end...

It should look something like this:

{ "membership":   "Premium", 
  "telephone":    "555636477", 
  "firstName":    "John",
  "lastName":     "Doe",
  "otherNumbers": [
                   {"number:"55687878"},
                   {"number":"9789899"},
                   {"number":"2899298"}
                  ]
}

2 Answers 2

2

Are this last numbers seperated through sprace characters? If so, you can use the split method, to get an array of Strings.

Then you have to use a RegEx for the first entry, all others are your telephone numbers.

The array would look like:

["P555636477JohnDoeEA", "55687878", "9789899", "2899298"]

Then you could use json-lib or an other library to get your data to JSON.

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

3 Comments

Well, there can be spaces between any element.
The sequence is same: membership, name, language... etc... but there might be spaces in between. Last telephone numbers are always separated with spaces. So it can look like this also: "P 555636477 JohnDoeE A 55687878 9789899 2899298"
If the speaces between the first data elements not always there you should parse this with an regex like: (P|G|S)( )?(\w)*( )? ... and always care about the optional space characters and make them non optional at the end.
1

Easiest would be to make a object which holds this information, parse your text file with a regexes, set the appropriate properties of the object and then serialize it with jackson.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.