I have sample data in this form:
{
ID:2
Type:None
Reference1:6369-545.1
Reference2:6369-545.2
Name:"John Smith Abbott"
Extra:""
},
{
ID:3
Type:None
Reference1:7854-568.1
Reference2:6369-545.2
Name:"Dave St. Ledger"
Extra:""
},
{
ID:4
Type:None
Reference1:8765-177.1
Reference2:6369-545.2
Name:"Martha Stone"
Extra:""
},
{
ID:6
Type:None
Reference1:9856-487.1
Reference2:6369-545.2
Name:"Peter O'Neill"
Extra:""
},
There is a lot more records, but I’ve only put up a small sample, and changed some names.
In my opinion, this is pretty awful formatting. Its not compatible with JSON parsers in java because the fields aren’t surrounded by double quotes, and I can’t do a mass find/replace.
Also, the end of each record isn’t separated by a comma, so while I can get each record into a String[], I can’t separate them out any further. See here:
ID:2 Type:None Reference1:6369-545.1 Reference2:6369-545.2 Name:"John Smith Abbott" Extra:""}
Usually I would split() by spaces, but as there are spaces in the peoples names, that can’t be done.
I did a mass find/replace on }, to replace with }; and like I said above, I was able to separate out each record, but then couldn’t get any further. I think I might need to do more find/replace operations on this data to make it usuable. Ideally I’d like to be able to add a comma at the end of each line of data, without adding one to the existing }, and { lines.
The ultimate goal is to insert this data into the database.
Its raw text and its in this format. I can’t change the source to reformat it, but I can do whatever I want to this data, as its in a big text file.
Thats not so difficult:
Just read the file line by line:
if you read a “{” call a readObject() method, which reads till next (including) “}”.
In this readObject:
You read line by line, and use
String.split()with delimiter “:”;Such that the first is stored in a varaible key, and the value in val.
Now either find the member to be set by comparing the key to all possibilities, or use reflection to get the field named “key”.