Home > Back-end >  Converting a JSON file to HTML in Java
Converting a JSON file to HTML in Java

Time:09-23

I'm new to Java and trying to convert any given json file to an html file without using any external libraries, except GSON to interpret the json structure. As I'm currently stuck in not understanding the proper way to approach this problem, I would like to humbly ask for your help.

This is an example json file:

{
    "doctype": "html",
    "language": "en",
    "head": {
        "meta": {
            "charset": "utf-8",
            "viewport": {
                "width": "device-width",
                "initial-scale": 1
            }
        },
        "title": "Page Not Found"
    },
    "body": {
        "h1": "Page Not Found",
        "p": "Sorry, but the page you were trying to view does not exist."
    }
}

And this is the expected output of the file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Page Not Found</title>
    </head>
    <body>
    <h1>Page Not Found</h1>
        <p>Sorry, but the page you were trying to view does not exist.</p>
    </body>
</html>

This is the code I have so far: (The actual conversion method)

private static void getJsonData (JsonObject jsonObject) throws IOException {
        jsonObject.keySet().forEach(key ->
        {
            Object value = jsonObject.get(key);

            if (value instanceof JsonObject){
                try {
                    getJsonData((JsonObject) value);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("key "   key   " of value "   value   " is a JsonObject");
            } else if (value instanceof JsonArray){
                try {
                    writeToHtmlFile("<"   key   ">");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("key "   key   " of value "   value   " is a JsonArray");
            } else {
                if (Objects.equals(key, "doctype")) {
                    try {
                        writeToHtmlFile("<!DOCTYPE "   value   ">");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else if (Objects.equals(key, "language")) {
                    try {
                        writeToHtmlFile("<html lang="   value   ">");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        writeToHtmlFile("<"   key   ">");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("key "   key   " of value "   value   " is an entry");
            }
            ;
        });
        ;
    }
    ;

(the json extract method using GSON)

public static Map readJsonFile (String path) {
        Map<String, String> map = null;
        try {
            Gson gson = new Gson();
            Reader reader = Files.newBufferedReader(Paths.get(path));
            map = gson.fromJson(reader, Map.class);
            reader.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return map;
    }

The method for html file writing:

private static void writeToHtmlFile (String toAdd) throws IOException {
        FileWriter fw = new FileWriter("output.html", true);
        fw.write(toAdd);
        fw.write("\n");
        fw.close();
    }

And this is my current output to an html file, which is obviously incorrect:

<!DOCTYPE "html">
<html lang="en">
<charset>
<width>
<initial-scale>
<title>
<h1>
<p>

CodePudding user response:

First of all, you are not writing HTML elements closing tags which should be wrapping any JsonObject:

if (value instanceof JsonObject) {
    writeToHtmlFile("<"   key   ">");
    try {
        getJsonData((JsonObject) value);
    } catch (IOException e) {
        e.printStackTrace();
    }
    writeToHtmlFile("</"   key   ">");
    System.out.println("key "   key   " of value "   value   " is a JsonObject");
}

Second you are not writing the literal HTML elements values which should go last if the node is not a JsonObject:

} else {
    writeToHtmlFile("<"   key   ">");
    writeToHtmlFile(value.toString());
    writeToHtmlFile("</"   key   ">");
}

The whole method would then look like the following:

public void getJsonData(JsonObject jsonObject) throws IOException {
    jsonObject.keySet().forEach(key ->
    {
        Object value = jsonObject.get(key);
        if (value instanceof JsonObject) {
            try {
                writeToHtmlFile("<"   key   ">");
                getJsonData((JsonObject) value);
                writeToHtmlFile("</"   key   ">");
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("key "   key   " of value "   value   " is a JsonObject");
        } else if (value instanceof JsonArray) {
            try {
                writeToHtmlFile("<"   key   ">");
                for (JsonElement next : (JsonArray) value) {
                    getJsonData(next.getAsJsonObject());
                }
                writeToHtmlFile("</"   key   ">");
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("key "   key   " of value "   value   " is a JsonArray");
        } else {
            if (Objects.equals(key, "doctype")) {
                try {
                    writeToHtmlFile("<!DOCTYPE "   value   ">");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (Objects.equals(key, "language")) {
                try {
                    writeToHtmlFile("<html lang="   value   ">");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    writeToHtmlFile("<"   key   ">");
                    writeToHtmlFile(value.toString());
                    writeToHtmlFile("</"   key   ">");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("key "   key   " of value "   value   " is an entry");
        }
    });
}

CodePudding user response:

Why would you need this? You can't add like h1: Some text, h2: some text and then again h1: a text, because json wouldn't allow that, so why you want to use your own html language? Which would be more limited as the original?

  • Related