JSON - The New Data Interchange Format For Web Services
What is JSON ?
JSON ( Javascript Object Notation) is a plain - text format for data storage. It has become quite popular as a data interchange format for web services, for configuration files and more. Features of JSON are, it is easy to read for humans and easy to parse by machines when compared to XML.
JSON stores data as plain text. Its grammar is subset of the grammar of java script expressions.
How to write a simple JSON object ?
Writing a JSON object is very simple. All the members of JSON are nothing but name, value pairs. Its some what simillar to Hash Map of Java language. Just begin with a '{' and all the name, value pairs will be seperated by ',' & name , value are seperated by ':'. Here is an example.
{
"First Name" : "speaking" ,
"Last Name" : "cs",
"Type" : "Blog",
"Domain" : "www.speakingcs.com"
}
You can save a JSON object as a file with ".json" extension, just like xml whose extension is ".xml".
The grammar behind creation of JSON is as follows.
1. Strings must always be double - quoted, string literals such as 'mystr' are illegal.
2. Property keys must be double - quoted.
How to create a JSON object in Java script ?
Simply follow the grammar, rules mentioned above & create a JSON object and assign it to a variable in java script. You can get values from JSON object using . operator.
Eg: var a = {
"first":"speaking",
"last":"cs",
"type":"Blog",
"domain":"www.speakingcs.com"
}
Retrieving values from JSON object :
a.first gives result as speaking, you can also get all the values by using "." operator and key name.
How to create a JSON object in Java ?
You can create JSON object in java using external APIs such as org.json.simple API or Google's gson API. Here is the sample code written by using org.json.simple API. The class used to create JSON object is "JSONObject". The method createJson() creates JSON object and saves to disk by using FileWriter class in java, the method readJSON() reads JSON object from disk using FileReader class in java.
JSON ( Javascript Object Notation) is a plain - text format for data storage. It has become quite popular as a data interchange format for web services, for configuration files and more. Features of JSON are, it is easy to read for humans and easy to parse by machines when compared to XML.
JSON stores data as plain text. Its grammar is subset of the grammar of java script expressions.
How to write a simple JSON object ?
Writing a JSON object is very simple. All the members of JSON are nothing but name, value pairs. Its some what simillar to Hash Map of Java language. Just begin with a '{' and all the name, value pairs will be seperated by ',' & name , value are seperated by ':'. Here is an example.
{
"First Name" : "speaking" ,
"Last Name" : "cs",
"Type" : "Blog",
"Domain" : "www.speakingcs.com"
}
You can save a JSON object as a file with ".json" extension, just like xml whose extension is ".xml".
The grammar behind creation of JSON is as follows.
Rules to follow while creating a JSON object :object
{} - which means a JSON object can be an empty object
{ members } - or an object with members in it
members
pair - which means pair is a name - value pair - so an JSON object can contain only one pair
pair, members - or multiple pairs
pair
string : value - pair is nothing but name - value association just like HashMap
array
[] - The value can be an empty array
[elements] - or with elements in int like numbers, strings, arrays, JSON objects
elements
value
value, elements
value
string - Which means a Value can be string ,number, another JSON object, array, boolean , null
number
object
array
true
false
null
string
"" - String can be empty string
"chars" - or non empty string
chars
char - a single character
char chars - or multiple characters
char
any Unicode character except \b \f \n \r \t \u four hex digits
number
int - number can be a normal integer
int frac - or a fraction just like Double or Float value in java
int exp - or an exponention
int frac exp - or combination of fraction and exponention e.g 14.345e+19
int
digit - an integer can be single digit
digit 1-9 digits - or a number
frac
.digits - fraction is a decimal number
exp
e digits
digit
digit
digit digits
e
e e+ e-
E E+ E
1. Strings must always be double - quoted, string literals such as 'mystr' are illegal.
2. Property keys must be double - quoted.
How to create a JSON object in Java script ?
Simply follow the grammar, rules mentioned above & create a JSON object and assign it to a variable in java script. You can get values from JSON object using . operator.
Eg: var a = {
"first":"speaking",
"last":"cs",
"type":"Blog",
"domain":"www.speakingcs.com"
}
Retrieving values from JSON object :
a.first gives result as speaking, you can also get all the values by using "." operator and key name.
How to create a JSON object in Java ?
You can create JSON object in java using external APIs such as org.json.simple API or Google's gson API. Here is the sample code written by using org.json.simple API. The class used to create JSON object is "JSONObject". The method createJson() creates JSON object and saves to disk by using FileWriter class in java, the method readJSON() reads JSON object from disk using FileReader class in java.
package speakingcs.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonWrite {
public static void main(String[] args) {
JsonWrite jw = new JsonWrite();
try {
jw.createJson();
jw.readJson();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void createJson() throws IOException {
JSONObject jsonObj = new JSONObject();
jsonObj.put("site name", "www.speakingcs.com");
jsonObj.put("type", "blog");
jsonObj.put("domain type", ".com");
jsonObj.put("dob","22-12-1990");
JSONArray dob = new JSONArray();
dob.add(22);
dob.add("december");
dob.add(1990);
JSONObject dateObj = new JSONObject();
dateObj.put("date", 22);
dateObj.put("month", "December");
dateObj.put("year", 1990);
jsonObj.put("DOB",dob);
jsonObj.put("dateObject", dateObj);
// saving file to disk
FileWriter fw = new FileWriter("simple.json");
fw.write(jsonObj.toJSONString());
fw.flush();
fw.close();
}
private void readJson() throws FileNotFoundException, IOException, ParseException {
JSONParser jParser = new JSONParser();
Object obj = jParser.parse(new FileReader("simple.json"));
// convert the obj into JSON Object
JSONObject jsonObj = (JSONObject) obj;
System.out.println(jsonObj.get("site name"));
JSONArray dobArray = (JSONArray) jsonObj.get("DOB");
Iterator<Object> itr = dobArray.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Comments
Post a Comment