0

I have three String[][] arrays that can be of different lengths. The second length is always fixed, and has length of 6, but the first length can differ between 0 and 6.

I want to create a Json-string containing the data of all of these String arrays. As of now it's hard coded, but i realize I'll get in to trouble once the lengths isn't fixed anymore...

travelgl1.put("Duration", str1[0][0]);
travelgl1.put("Walking time", str1[0][1]);
travelgl1.put("Direction", str1[0][2]);
travelgl1.put("Departure", str1[0][3]);
travelgl1.put("Arrival", str1[0][4]);
travelgl1.put("End station", str1[0][5]);

So basically I want a dynamic loop that loops through the String arrays and adds all the data in that array.

Is there any easy way to do this? I want all three json objects (from the three String arrays) to be compiled in to one big json string in the end.

I want the end result to look like this:

[ { "String array 1" : [ { "Duration" : "33", "Walking time" : "8", "Direction" : "Åkeshov", "Departure" : "09:39", "Arrival" : "10:43", "End station" : "Sollentuna" }, { "Duration" : "37", "Walking time" : "8", "Direction" : "Alvik", "Departure" : "09:43", "Arrival" : "10:51", "End station" : "Sollentuna" }, { "Duration" : "34", "Walking time" : "8", "Direction" : "Alvik", "Departure" : "09:53", "Arrival" : "10:58", "End station" : "Sollentuna" }, { "Duration" : "36", "Walking time" : "8", "Direction" : "Åkeshov", "Departure" : "09:59", "Arrival" : "11:06", "End station" : "Sollentuna" }, { "Duration" : "33", "Walking time" : "8", "Direction" : "Åkeshov", "Departure" : "10:09", "Arrival" : "11:13" } ] }, { "String array 2" : [ { "Duration" : "54", "Walking time" : "13", "Direction" : "Farsta strand", "Departure" : "09:43", "Arrival" : "11:13", "End station" : "Sollentuna" }, { "Duration" : "47", "Walking time" : "13", "Direction" : "Gullmarsplan", "Departure" : "09:50", "Arrival" : "11:13", "End station" : "Sollentuna" }, { "Duration" : "45", "Walking time" : "13", "Direction" : "Gullmarsplan", "Departure" : "10:00", "Arrival" : "11:21", "End station" : "Sollentuna" }, { "Duration" : "42", "Walking time" : "13", "Direction" : "Gullmarsplan", "Departure" : "10:10", "Arrival" : "11:28", "End station" : "Sollentuna" }, { "Duration" : "45", "Walking time" : "13", "Direction" : "Gullmarsplan", "Departure" : "09:30", "Arrival" : "10:51", "End station" : "Sollentuna" } ] }, { "String array 3" : [ { "Duration" : "31", "Walking time" : "12", "Direction" : "Hässelby strand", "Departure" : "09:45", "Arrival" : "10:51", "End station" : "Sollentuna" }, { "Duration" : "31", "Walking time" : "12", "Direction" : "Alvik", "Departure" : "09:52", "Arrival" : "10:58", "End station" : "Sollentuna" }, { "Duration" : "33", "Walking time" : "12", "Direction" : "Åkeshov", "Departure" : "09:58", "Arrival" : "11:06", "End station" : "Sollentuna" }, { "Duration" : "30", "Walking time" : "12", "Direction" : "Åkeshov", "Departure" : "10:08", "Arrival" : "11:13", "End station" : "Sollentuna" }, { "Duration" : "31", "Walking time" : "12", "Direction" : "Hässelby strand", "Departure" : "10:15", "Arrival" : "11:21", "End station" : "Sollentuna" } ] } ]

So, what I really want is a dynamic way of creating a Json string depending on the length of the string array.

Thanks in advance!

1 Answer 1

2

please take the reference to the below code.

public List<Map<String, String>> myfunction(String str1[][]) {
    List<Map<String, String>> travelgl1 = new ArrayList<Map<String, String>>();
    for (int i = 0; i < str1.length; i++) {
        Map<String, String> map = new HashMap<String, String>();
        for (int j = 0; j < 6; j++) {
            if (j == 0)
                map.put("Duration", str1[i][j]);
            else if (j == 1)
                map.put("Walking time", str1[i][j]);
            else if (j == 2)
                map.put("Direction", str1[i][j]);
            else if (j == 3)
                map.put("Departure", str1[i][j]);
            else if (j == 4)
                map.put("Arrival", str1[i][j]);
            else if (j == 5)
                map.put("End station", str1[i][j]);
        }
        travelgl1.add(map);
    }
    return travelgl1;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.