The code below is taking a string (that happens to be JSON) - converting it to a JSONObject and then iterating through the data. The JSON has a schema(column) and data. My end goal is to have each of the items linked and then put into the map.
This works perfectly when each item is distinct, like the following :
String jsonSTR = "{\"schema\":[\"col1\",\"col2\",\"col3\"],\"data\":[[\"banana\",\"1\",face],[\"orange\",\"2\",\"foot\"],[\"apple\",\"3\",\"hand\"]]}";
Output
{banana=col1, orange=col1, 1=col2, apple=col1, face=col3, 2=col2, 3=col2, foot=col3, hand=col3}
But the second duplicates are introduced (that is included in the code below) I end up overwriting values. I tried switching the values for the input into the list, but the same issue exists.
I'm either using the wrong containers to hold this data, or I am using them incorrectly. I thought LISTS could hole duplicates.
Here's my current code :
import org.json.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
ArrayList<String> schemaList = new ArrayList<>();
List<Map<String, Object>> allData = new ArrayList<Map<String, Object>>();
Map<String,Object> partData = new HashMap<String, Object>();
String jsonSTR = "{\"schema\":[\"col1\",\"col2\",\"col3\"],\"data\":[[\"banana\",\"orange\",2],[\"face\",\"arm\",\"eye\"],[\"yes\",\"no\",\"no\"]]}";
JSONObject jsonObj = new JSONObject(jsonSTR);
//Getting schema
JSONArray schema = jsonObj.getJSONArray("schema");
for (int i = 0; i < schema.length(); i++)
{
schemaList.add(schema.get(i).toString());
}
//Getting data
JSONArray data = jsonObj.getJSONArray("data");
for (int j = 0; j < data.length(); j++)
{
JSONArray row = data.getJSONArray(j);
for (int k = 0; k < row.length(); k++)
{
partData.put(schema.get(k).toString(),row.get(k));
allData.add(partData);
}
}
System.out.println(allData.get(0));
System.out.println(allData.get(1));
}
}
If the data has to be in the map with the key, then no data can be repeated in any column, so it is not the best solution.
public static Map<String, List<String>> toMap(String jsonSTR) {
Map<String, List<String>> result = new HashMap<>();
JSONObject jsonObj = new JSONObject(jsonSTR);
JSONArray schema = jsonObj.getJSONArray("schema");
JSONArray data = jsonObj.getJSONArray("data");
for (int i = 0; i < schema.length(); i++) {
String key = schema.get(i).toString();
result.put(key, new ArrayList<>());
for (int j = 0; j < data.length(); j++)
result.get(key).add(((JSONArray)data.get(j)).get(i).toString());
}
return result;
}
In:
"{\\"schema\\":[\\"col1\\",\\"col2\\",\\"col3\\",\\"col4\\"],\\"data\\":[[\\"banana\\",\\"1\\",\\"face\\",\\"black\\"],[\\"orange\\",\\"2\\",\\"foot\\",\\"red\\"],[\\"apple\\",\\"3\\",\\"hand\\",\\"yellow\\"]]}"
Out:
{col4=[black, red, yellow], col2=[1, 2, 3], col3=[face, foot, hand], col1=[banana, orange, apple]}
What do you think about this solution?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.