Donatello
Donatello

Reputation: 353

How to get String[] array of JSON objects in java from String JSON that is contained an array without modification

I am getting String jsonObject in my controller. The structure is following:

{ "cats":
    [
        {
  "name": "Smoky",
  "age": 12,
  "color": "gray"
},
        {
  "name": "Oscar",
  "age": 3,
  "color": "black"
},
       {
  "name": "Max",
  "age": 4,
  "color": "white"
}
    ]
}

I need to parse it into String[] jsonObjects or List<String> jsonObjects.
Using GSON I am trying to do it this way:

public static String[] toArray(String json) {
        final String PARSING_ERROR = "Error while parsing json to string array";
        try {
            JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
            String tableName = jsonObject.keySet().toArray()[0].toString();
            JsonArray jsonArray = jsonObject.getAsJsonArray(tableName);
            String[] strings = new String[jsonArray.size()];
            for (int i = 0; i < jsonArray.size(); i++) {
                String stringJson = jsonArray.get(i).toString();
                strings[i] = stringJson;
            }
            return strings;
        } catch (Exception e) {
            System.err.println(PARSING_ERROR);
            throw new DataException(PARSING_ERROR);
        }
    }

It works, but after parsing I recieve the following String:

{"name":"Smoky","age":12,"color":"gray"}

How can I get the String in the following format:

{
  "name": "Smoky",
  "age": 12,
  "color": "gray"
}

Upvotes: 0

Views: 647

Answers (2)

xerx593
xerx593

Reputation: 13281

Sorry, this is not the correct answer for PO's question, but might be helpful for other users...who want to use GSON to (pretty) serialize object, so String jsonOutput = gson.toJson(someObject);

You "just" need to:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(jsonObject);

See here.

Upvotes: 1

xerx593
xerx593

Reputation: 13281

In your case, you only use GSON, to read (deserialize) from JSON:

JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);

For the resulting String[] and its output, your program is responsible!

...and if you don't want to re-invent the wheel, when like the "pretty" formatting & since have GSON "on board", you can do:

public static String[] toArray(String json) {
    final String PARSING_ERROR = "Error while parsing json to string array";
    // one for toJson() (only)
    final Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create();
    // one for fromJson (demo only)
    final Gson gsonUgly = new Gson();
    try {
        JsonObject jsonObject = gsonUgly.fromJson(json, JsonObject.class);
        String tableName = jsonObject.keySet().toArray()[0].toString();
        JsonArray jsonArray = jsonObject.getAsJsonArray(tableName);
        String[] strings = new String[jsonArray.size()];
        for (int i = 0; i < jsonArray.size(); i++) {
            // de-serialize & then pretty serialize each "cat".
            String catJson = jsonArray.get(i).toString();
            JsonObject catObj = gsonUgly.fromJson(catJson, JsonObject.class);
            strings[i] = gsonPretty.toJson(catObj);
        }
        return strings;
    } catch (Exception e) {
        System.err.println(PARSING_ERROR);
        throw new DataException(PARSING_ERROR);
    }
}

Upvotes: 0

Related Questions