Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

So I have a method.

public String getWidgetConfigAsString(List<DashboardColumn> dashboardList) {
        Gson gson = new Gson();
        JsonArray widgetConfigJson = gson.toJsonTree(dashboardList).getAsJsonArray();

        for (int i = 0; i < widgetConfigJson.getAsJsonArray().size(); i++) {
            System.out.println(widgetConfigJson.getAsJsonArray().get(i));
        }

        return widgetConfigAsString;
    }

And this method returns :

{"widgets":["notary_registry","notary_tasks"]}
{"widgets":["calendar"]}
{"widgets":[]}

This is a JSF dashboard layout, basicly first row is first column of a dashboard and so on.

What I want to achieve is, to get just regular multi-Array which would give me output like :

{["notary_registry","notary_tasks"]}
{["calendar"]}
{[]}
share|improve this question
    
And by returns you mean prints in output window? JSON is name-value so if you get an item and print it you would expect to get name-value. Try using widgetConfigJson.getAsJsonArray().get(i).valueToString(), then you can add {[]} appropriately for your needs. – Mattias Lindberg yesterday
    
println() is just for me now, to see the output, I want to store output to String [] [] and using toString(); doesn't change anything now. – Tomas Šiaudvytis yesterday
    
So why not share the code you have tried to use to create your output? You shared code printing to std out and returning empty string, and hence you get a response to that. Share your code and you will surely get help adjusting it to match your requirement. But start by trying to use valueToString to get each individual value. – Mattias Lindberg yesterday
    
yeah.. I tried some "weird" combinations, like spliting this JsonArray into single dimension Arrays.. I could get the output I need, with weidly written code, but I am looking for someting efficient, if there is. and.. valueToString() is give me compile error. – Tomas Šiaudvytis yesterday

1 Answer 1

Looks ugly, but.. does the job, will find someting, that looks good enough to use.. :)

for (int i = 0; i < widgetConfigJson.size(); i++) {

            if (widgetConfigJson.get(i).toString().contains("widgets")) {
                int a = widgetConfigJson.get(i).toString().indexOf('[');
                int b = widgetConfigJson.get(i).toString().indexOf(']');

                System.out.println(widgetConfigJson.get(i).toString().substring(a, b + 1));
            }
share|improve this answer
    
ok, so I mised in documentation, there is nice method that does what I needed.. public String getWidgetConfigAsString(List<DashboardColumn> dashboardList) { Gson gson = new Gson(); List<List<String>> newList = new ArrayList<>(); for (int i = 0; i < dashboardList.size(); i++) { newList.add(dashboardList.get(i).getWidgets()); } return gson.toJson(newList); } – Tomas Šiaudvytis yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.