Coding Fun Java Java – write a JSON file

Java – write a JSON file





Java – write a JSON file

Tags: , , , , ,

16 thoughts on “Java – write a JSON file”

  1. Everything is "nice & clear" ! Couldn't be more simple ! Thank you. Keep on with such a practical examples.

  2. atul arya says:

    where will i get the created json file ?

  3. How to append the json file ? I am writing some performance logs and at run time i want to append the output

  4. Sashimi says:

    THANK YOU! Spent 2 hrs trying to use Maven with Netbeans and could not get the damn import to work. This was so much easier. Lol.

  5. alex chile says:

    I am trying to write from mysql a json file. I do not know what is wrong with my code. When I write

    http://localhost:8085/Json3/testjson.jsp the information apprears as follows:

    [{"Nombre":"Nancy","Cargo":"Sales Representative","Empresa":"Northwind Traders"},{"Nombre":"Andrew","Cargo":"Vice President, Sales","Empresa":"Northwind Traders"},{"Nombre":"Jan","Cargo":"Sales Representative","Empresa":"Northwind Traders"},{"Nombre":"Mariya","Cargo":"Sales Representative","Empresa":"Northwind Traders"},{"Nombre":"Steven","Cargo":"Sales Manager","Empresa":"Northwind Traders"},{"Nombre":"Michael","Cargo":"Sales Representative","Empresa":"Northwind Traders"},{"Nombre":"Robert","Cargo":"Sales Representative","Empresa":"Northwind Traders"},{"Nombre":"Laura","Cargo":"Sales Coordinator","Empresa":"Northwind Traders"},{"Nombre":"Anne","Cargo":"Sales Representative","Empresa":"Northwind Traders"},{"Nombre":"Alex","Cargo":"ing.","Empresa":"acme"}]

    But I need to write this information json in a .json file. Before I creted a blank file "nuevo.json" and is located as follows: (Netbeans)

    Json3

    webpages

    index.html

    nuevo.json

    testjson.jsp

    testjson.jsp contains the following:

    <%@page import="java.io.IOException"%>

    <%@page import="javax.json.Json"%>

    <%@page import="javax.json.JsonObject"%>

    <%@page import="java.util.ArrayList"%>

    <%@page import="java.io.Writer"%>

    <%@page import="java.io.File"%>

    <%@page import="java.io.FileWriter"%>

    <%@page import="java.util.logging.Logger"%>

    <%@page import="java.util.logging.Level"%>

    <%@ page import="java.lang.*"%>

    <%@ page import="java.sql.*"%>

    <%@page import="org.json.simple.*"%>

    <%

    JSONObject obj;

    JSONArray array;

    try {

    Class.forName("com.mysql.jdbc.Driver");

    Connection dbConnection = DriverManager.getConnection(

    "jdbc:mysql://localhost:3306/northwind", "root", "");

    Statement getFromDb = dbConnection.createStatement();

    ResultSet employees = getFromDb.executeQuery("SELECT first_name, company,job_title FROM employees");

    array = new JSONArray();

    while (employees.next()) {

    obj = new JSONObject();

    obj.put("Nombre", employees.getString("first_name"));

    obj.put("Empresa", employees.getString("company"));

    obj.put("Cargo", employees.getString("job_title"));

    array.add(obj);

    }

    out.println(array);

    try {

    FileWriter file = new FileWriter("nuevo.json");

    file.write(((JSONArray) array).toJSONString());

    file.flush();

    file.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    } catch (SQLException ex) {

    // handle any errors

    System.out.println("SQLException: " + ex.getMessage());

    System.out.println("SQLState: " + ex.getSQLState());

    System.out.println("VendorError: " + ex.getErrorCode());

    }

    %>

    When I see the "nuevo.json" is empty……….

  6. alex chile says:

    I am trying to create a json file but reading the data from a mysql table. Can you do that??????

  7. Very helpful, I used it as a DataBase.

  8. sana amjad says:

    error -> filewriter could not resolve to a type

  9. DeNy Javiier says:

    Thanks Saad. Greeting from Mexico.

  10. i am getting error in the import org.json.simple.JSONObject; package and where to keep the JSON file ????

  11. Ninos Ochana says:

    How can I splite it in many lines

  12. Yifan Xu says:

    where is that myJSON.json saved so that when I use fileReader I am able to pass the name as the parameter?

  13. PERFECT.
    WHY NO ONE ELSE UPLOAD A SIMPLE AND SPECIFIC CONTENT SUCH LIKE THIS ?

  14. DJ Rafa says:

    How do I get rig of the " " in the key

  15. Where is the FileWriter class? Im confused

  16. Jacky_Tan says:

    how to append data without replacing it?

Leave a Reply

Your email address will not be published. Required fields are marked *