{"name" : "Mr. Srikanth", "occupation" : "Director", "company" : "Srikanth Technologies"}
{ "name" : "Mr. Srikanth", "occupation" : "Director", "company" : "Srikanth Technologies", "contacts" : [ {"mobile" : "9059057000"}, { "email" : "srikanthpragada@yahoo.com"}] }
import java.io.FileWriter; import java.io.StringWriter; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; import javax.json.JsonWriter; public class CreatJsonObject { public static void main(String[] args) throws Exception { JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add("name", "Srikanth"); builder.add("occupation", "Director"); builder.add("company", "Srikanth Technologies"); JsonObject person = builder.build(); StringWriter sw = new StringWriter(); JsonWriter jw = Json.createWriter(sw); jw.writeObject(person); jw.close(); System.out.println(sw.toString()); } }
{"name":"Srikanth","occupation":"Director","company":"Srikanth Technologies"}
// write JSON object to a file -> c:\java\person.txt FileWriter fw = new FileWriter("c:\\java\\person.txt"); JsonWriter fjw = Json.createWriter(fw); fjw.writeObject(person); fjw.close(); fw.close(); System.out.println("Wrote Object To File");
import java.io.StringWriter; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; import javax.json.JsonWriter; public class CreatJsonArray { public static void main(String[] args) { JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add("name", "Srikanth"); builder.add("occupation", "Director"); builder.add("company", "Srikanth Technologies"); JsonArray contacts = Json.createArrayBuilder().add( Json.createObjectBuilder().add("email", "srikanthpragada@yahoo.com")) .add(Json.createObjectBuilder().add("mobile", "9059057000")) .build(); builder.add("contacts", contacts); // contacts array JsonObject person = builder.build(); StringWriter sw = new StringWriter(); JsonWriter jw = Json.createWriter(sw); jw.writeObject(person); jw.close(); System.out.println(sw.toString()); } // main }
{"name":"Srikanth","occupation":"Director","company":"Srikanth Technologies","contacts":[{"email":"srikanthpragada@yahoo.com"},{"mobile":"9059057000"}]}
import java.io.StringWriter; import javax.json.Json; import javax.json.stream.JsonGenerator; public class UsingGenerator { public static void main(String[] args) { StringWriter sw = new StringWriter(); JsonGenerator gen = Json.createGenerator(sw); gen.writeStartObject() .write("name", "Srikanth Pragada") .write("email", "srikanthpragada@yahoo.com") .write("mobile", "34343434343") .writeEnd(); gen.close(); System.out.println(sw.toString()); } }
import java.io.FileWriter; import javax.json.Json; import javax.json.stream.JsonGenerator; public class WriteArrayWithGenerator { public static void main(String[] args) throws Exception { FileWriter writer = new FileWriter("c:\\java\\persondetails.txt"); JsonGenerator gen = Json.createGenerator(writer); gen.writeStartObject() .write("name", "Srikanth") .write("occupation", "Director") .write("company", "Srikanth Technologies") .writeStartArray("contacts") .writeStartObject() .write("email", "srikanthpragada@yahoo.com") .writeEnd() .writeStartObject() .write("mobile", "9059057000") .writeEnd() // for object .writeEnd() // for array .writeEnd(); // for root object gen.close(); } }
import java.io.FileReader; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonReader; public class DisplayPersonDetailsWithObjectModel { public static void main(String[] args) throws Exception { JsonReader reader = Json.createReader(new FileReader("c:\\java\\persondetails.txt")); JsonObject person = reader.readObject(); System.out.println("Name : " + person.getString("name")); System.out.println("Occupation : " + person.getString("occupation")); System.out.println("Company : " + person.getString("company")); // display contacts JsonArray contacts = person.getJsonArray("contacts"); JsonObject jobj = contacts.getJsonObject(0); System.out.println("Email : " + jobj.getString("email")); jobj = contacts.getJsonObject(1); System.out.println("Mobile : " + jobj.getString("mobile")); } }
Name : Srikanth Occupation : Director Company : Srikanth Technologies Email : srikanthpragada@yahoo.com Mobile : 9059057000
import java.io.FileReader; import javax.json.Json; import javax.json.stream.JsonParser; public class DisplayPersonWithStreamAPI { public static void main(String[] args) throws Exception { JsonParser parser = Json.createParser(new FileReader("c:\\java\\person.txt")); while (parser.hasNext()) { JsonParser.Event event = parser.next(); switch (event) { case KEY_NAME: System.out.print( parser.getString() + ":"); break; case VALUE_STRING: System.out.println(parser.getString()); break; } } } }