Question

In: Computer Science

Java 1. public void writeJSON(PrintStream ps) { Write the member variables in JSON format to the...

Java

1. public void writeJSON(PrintStream ps) {

Write the member variables in JSON format to the given PrintStream

}

2. public void writeJSON(PrintStream ps){

Write the member variables in JSON format to the given PrintStream.

}

Solutions

Expert Solution

Here I have created a dummy JSON string to write it is a proper json format.

You can specify a path and file name where you want to store the JSON formatted Data

package com.kontrolscan.services.impl;

import java.io.IOException;
import java.io.PrintStream;

import org.json.JSONException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Testing {

        public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
                PrintStream ps = new PrintStream("/home/amity/Desktop/atesting.txt");
                writeJSON(ps);
        }

        public static void writeJSON(PrintStream ps) throws JsonParseException, JsonMappingException, IOException {

                String s = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}";
                try {
                        String formattedJSON = beautify(s);
                        ps.print(formattedJSON);
                        ps.close();
                } catch (JSONException err) {
                        System.out.println("JSON Parsing error");
                }
        }
        public static String beautify(String jsonAsString) throws JsonParseException, JsonMappingException, IOException {
                ObjectMapper mapper = new ObjectMapper();
            Object obj = mapper.readValue(jsonAsString, Object.class);
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        }
}

Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
write a java program that allows main to work public static void main(String[] args) { /*...
write a java program that allows main to work public static void main(String[] args) { /* Start with the empty list. */ LinkedList list = newLinkedList(); // // ******INSERTION****** // // Insert the values deleteFront(list); deleteBack(list); list = insert(list, 1); list = insert(list, 2); list = insert(list, 3); list = insert(list, 4); list = insert(list, 5); list = insert(list, 6); list = insert(list, 7); list = insert(list, 8);    // Basic Operations on the LinkedList printList(list); insertFront(list,0); printList(list); insertBack(list,999); printList(list);...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField {...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField { public JTextField(){} public void setText(String text) {} public String getText() {} } Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2....
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2. Create a class Car. Implements Vehicle. It's paint() method prints "Car Painted". It's drive( ) method prints "Car Driven". 3. Create a class Bus. Implements Vehicle. It's paint() method prints "Bus Painted" . It's drive( ) method prints "Bus Driven".   4. Create a method AutoWork which accepts an object of type Vehicle. The method makes a call to the vehicle's paint( ) and drive()...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT