I have a JSON array in the following format:
[["1234","OS","01/31/2023","02/01/2023","First Day"],["1245","OS","01/23/2023","01/24/2023","Last Day"],["3411","OS","09/21/2022","09/21/2022","Second Day"]]
In Java, I would like to parse this array and store data in the following format:
String[] firstElements = ["1234" , "1245", "3411"];
String[] secondElements = ["OS", "OS", "OS"];
String[] thirdElements = ["01/31/2023", "01/23/2023", "09/21/2022"];
String[] fourthElements = ["02/01/2023", "01/24/2023", "09/21/2022"];
String[] fifthElements = ["First Day", "Last Day", "Second Day"];
Is this possible? How can I achieve this?
String jsonArray = "[[\"1234\",\"OS\",\"01/31/2023\",\"02/01/2023\",\"First Day\"],[\"1245\",\"OS\",\"01/23/2023\",\"01/24/2023\",\"Last Day\"],[\"3411\",\"OS\",\"09/21/2022\",\"09/21/2022\",\"Second Day\"]]";
JsonArray array = Json.createReader(new StringReader(jsonArray)).readArray();
List<String> firstElements = new ArrayList<>();
List<String> secondElements = new ArrayList<>();
List<String> thirdElements = new ArrayList<>();
List<String> fourthElements = new ArrayList<>();
List<String> fifthElements = new ArrayList<>();
array.forEach( value -> {
JsonArray innerArray = value.asJsonArray();
firstElements.add(innerArray.getString(0));
secondElements.add(innerArray.getString(1));
thirdElements.add(innerArray.getString(2));
fourthElements.add(innerArray.getString(3));
fifthElements.add(innerArray.getString(4));
)};
If you want something more generic you can use a List<List<String>>
String jsonArray = "[[\"1234\",\"OS\",\"01/31/2023\",\"02/01/2023\",\"First Day\"],[\"1245\",\"OS\",\"01/23/2023\",\"01/24/2023\",\"Last Day\"],[\"3411\",\"OS\",\"09/21/2022\",\"09/21/2022\",\"Second Day\"]]";
JsonArray array = Json.createReader(new StringReader(jsonArray)).readArray();
List<List<String>> elements = new ArrayList<>();
array.forEach( value -> {
JsonArray innerArray = value.asJsonArray();
List<String> subElements = new ArrayList<>();
for (int i = 0; i < innerArray.size(); i++) {
subElements.add(innerArray.getString(i));
}
elements.add(subElements);
});
There are a few solutions to parsing JSON from Java, like GSON, though if the data format you are working with is simple enough and you don't have to parse all kinds of JSON, you might as well parse it by yourself.
Is it possible to convert this type List<Jadval> into String[] wordList?
I read the words from database with like this :
public static List<Jadval> jadvalList = new ArrayList<Jadval>();
JadvalDB jadvalDB = new JadvalDB(GameActivity.this);
jadvalList = jadvalDB.getWords(myPos + 1);
and now i want to put jadvalList values into String[] wordList.
i use this code to set the values :
for (int i = 0; i < jadvalList.size(); i++) {
wordList[i] = (jadvalList.get(i).toString());
}
but I get the error that wordList is empty .
any idea?
You can use streams and complete it in one line like this:
String[] wordList = jadvalList.stream().map(a->a.toString()).toArray(String[]::new);
I'm trying to add to a 3d array as below:
ArrayList<ArrayList<ArrayList<String>>> bigList = new ArrayList<>();
ArrayList<ArrayList<String>> smallList = new ArrayList<>();
But when I add like below:
bigList.add(smallList);
It just overwrites previous entries
How can I fix?
You are a bit confused in your approach. Here is a working example of how to add ArrayLists to a 3d ArrayList:
ArrayList<ArrayList<ArrayList<String>>> masterList = new ArrayList<>();
ArrayList<ArrayList<String>> middleList1 = new ArrayList<>();
ArrayList<ArrayList<String>> middleList2 = new ArrayList<>();
ArrayList<String> innerList1 = new ArrayList<>();
ArrayList<String> innerList2 = new ArrayList<>();
ArrayList<String> innerList3 = new ArrayList<>();
ArrayList<String> innerList4 = new ArrayList<>();
innerList1.add("String 1");
innerList1.add("String 2");
innerList2.add("String 3");
innerList2.add("String 4");
innerList3.add("String 5");
innerList3.add("String 6");
innerList4.add("String 7");
innerList4.add("String 8");
middleList1.add(innerList1);
middleList1.add(innerList2);
middleList2.add(innerList3);
middleList2.add(innerList4);
System.out.println(masterList.size()); //output: 0
masterList.add(middleList1);
System.out.println(masterList.size()); //output: 1
masterList.add(middleList2);
System.out.println(masterList.size()); //output: 2
I'm not 100% sure, but see if this helps:
String[][][] bigList=new String[how"wide"][how"tall"][how"fat"]
I'm facing the problem and I did not find for any exact explanation for it. I am running the program in debug mode, I see that when the variable add to g_temNodes arraylist: g_tempNodes.add(p_dept_cd); , that variable also automatically add to g_nodes before the running g_nodes.add(g_tempNodes); .
Pls give me some ways to solve it. Thanks in advance.
my code is as the followings.
ArrayList<ArrayList<String>> g_nodes = new ArrayList<ArrayList<String>>();
ArrayList<String> g_tempNodes = new ArrayList<String>();
...
...
private String GetDeptCd(String x_dept_cd, int x_flag) {
...
...
while (p_sql.next()) {
String p_dept_cd = p_sql.getString("dept_cd");
if(x_flag == 0){
g_tempNodes.removeAll(g_tempNodes);
}
g_tempNodes.add(p_dept_cd);
System.out.println("g_tempNodes = "+g_tempNodes);
g_nodes.add(g_tempNodes);
System.out.println("g_nodes = "+g_nodes);
GetDeptCd(p_dept_cd, 1);
g_tempNodes.remove(g_tempNodes.size()-1);
}
return null;
}
This is the output in console.
g_tempNodes = [100]
g_nodes = [[100]]
g_tempNodes = [100, 999]
g_nodes = [[100, 999], [100, 999]]
g_tempNodes = [100, 101]
g_nodes = [[100, 101], [100, 101], [100, 101]]
Since you're using the same g_tempNodes always, the values are getting overwritten. Declare a new g_tempNodes in the loop always
ArrayList<ArrayList<String>> g_nodes = new ArrayList<ArrayList<String>>();
// ArrayList<String> g_tempNodes = new ArrayList<String>(); // not needed here
...
while (p_sql.next()) {
ArrayList<String> g_tempNodes = new ArrayList<String>(); // new list created always
...
}
I need to add a ResultSet to a list of lists. The string passed to the method is an SQL select statement. The DB connection methods work perfectly with all other methods in this class so that's not the problem here. I know I can replace some of the ArrayList declarations with List but I don't think that matters in this case.
public static ArrayList<ArrayList> selectStatement(String string) throws SQLException {
ArrayList<ArrayList> listOfLists = null;
ArrayList list;
String[] record = null;
try {
rs = null;
dBConnectionOpen();
rs = st.executeQuery(string);
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
while (rs.next()) {
list = null;
record = new String[columns];
for (int i = 1; i < columns; i++) {
record[i - 1] = rs.getString(i);
}
list = new ArrayList(Arrays.asList(record));
listOfLists.add(list);
}
} catch (Exception e) {
} finally {
dBConnectionClose();
}
return listOfLists;
}
I have done this before, but for some reason it just won't work this time. What am I missing here?
You initialize listOfLists with null value. Try instantiating it from the beginning:
ArrayList<ArrayList> listOfLists = new ArrayList<ArrayList>();
Also, it would be better:
Use List interface instead of plain ArrayList class implementation
Use List<String> instead of raw List.
Instead of using String[] record, save the data directly in the List<String>.
Keep the variable scope as short as possible. List<String> list can be directly inside the while loop.
Knowing this, the code can change to:
List<List<String>> listOfLists = new ArrayList<List<String>>();
...
while (rs.next()) {
List<String> list = new ArrayList<String>();
for (int i = 1; i < columns; i++) {
list.add(rs.getString(i));
}
listOfLists.add(list);
}
More info:
What does it mean to "program to an interface"?