So I have a situation where I am fetching some data from a database which I can't change/update. So my data from 2 columns coming like this:
For example:
Column1 Column2
Row 1: hello.how.are.you Gracie
Row 2: hello.how.is.she John
Row 3: hello.how.is.he Gurinder
Row 4: hello.from.me Singh
So I need to create a JSON which will look like:
{
"hello":{
"how":{
"are":{
"you":"Gracie"
},
"is":{
"he":"Gurinder",
"she":"John"
}
},
"from":{
"me":"Singh"
}
}
}
I want some optimize way to create my JSON. Thanks!
public static void main(String[] args) {
List<String > stringList = new ArrayList();
stringList.add("hello.how.are.you");
stringList.add("hello.how.is.she");
stringList.add("hello.how.is.he");
stringList.add("hello.from.me");
JSONObject response = new JSONObject();
for (String str : stringList) {
String[] keys = str.split("\\.");
for (int i = 0; i < keys.length; i++) {
if (response.has(keys[i])) {
} else {
JSONObject jsonObject2 = new JSONObject()
response.append(keys[i], jsonObject2);
}
}
}
}
I am doing something like this and trying to solve.