0

I have this data in my text file.

Obj1= {
    "AA" : "sasa",
    "BB" : "fdsfsf",
    "CC" : "sfsdf",
    "DD" : "kmdksmd",
    "EE" : "dsnjsdn"
};

Obj2= {
    "DD" : "ndjsdnsjd",
    "MM" : "jskdjskadn"
};

This data is in a single text file. How do I convert this to two different objects in JAVA

1 Answers1

0

Is it possible to use more friendly, fully JSON format instead? It might look like:

{
"Obj1" : {
    "AA" : "sasa",
    "BB" : "fdsfsf",
    "CC" : "sfsdf",
    "DD" : "kmdksmd",
    "EE" : "dsnjsdn"
},
"Obj2" : {
    "DD" : "ndjsdnsjd",
    "MM" : "jskdjskadn"
}
}

Then it would be very easy to load it like described in this thread

The file from your example isn't the correct JSON file so you can't do that so easily. If you really want to stick with your format, you would have to somehow tokenize/parse the file first to extract valid JSON fragments and than pass it to e.g. GSON library to convert them to objects.

If these will be small files like from your example, you can just load the whole file to string and try use regular expressions (with groups) and/or StringTokenizer to extract JSON chunks.

Community
  • 1
  • 1
Piotr Sobczyk
  • 6,443
  • 7
  • 47
  • 70