TECH

JavaScript Object Notation

JSON: JavaScript Object Notation

JSON file

Java Object Notation tools are libraries to use JSON with JAVA. SAJ (Simple API for JSON)=> An event parser for json.

JSON is syntax for storing and exchanging text information. Much like XML. JSON is smaller than XML, and faster and easier to parse.

JSON Example

{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

JSONPath
JSONPath is a means of using XPath-like syntax to query JSON structures. It is not a standard, but it does offer a means of querying JavaScript structures directly without needing to convert to or from XML.

JXON
JXON stands for lossless Javascript XML Object Notation, it is a generic name by which is defined the representation of Javascript object trees (JSON) using XML.

JSON.stringify method
Convert a value to JSON, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.

JSON.parse method
Parse a string as JSON, optionally transforming the value produced by parsing.

The simplest, easiest way to use JSON is through the standard JSON object in JavaScript.  Use JSON.stringify to serialize a JSON value, and use JSON.parse to deserialize a JSON value.

For example, to serialize a JavaScript object, use the JSON.stringify() method:

var foo = {};
foo.bar = "new property";
foo.baz = 3;
var JSONfoo = JSON.stringify(foo);

JSONfoo now holds {“bar”:”new property”,”baz”:3}. To make JSONfoo back into a JavaScript object just do:

var backToJS = JSON.parse(JSONfoo);

toJSON() method

If an object defines a toJSON() method, JSON.stringify will call that method to determine the object’s JSON representation. For example:

x = {};
x.foo = "foo";  
x.toJSON = function() { return "bar"; };
var json1 = JSON.stringify(x);

json1 now contains ‘”bar”‘.

Limitations

You cannot serialize an object that has members which are functions. The JSON specification does not allow it. For example:

foo.qwerty = function(){alert('foobar');};
foo.qwerty()
var JSONfoo = JSON.toString(foo);

will raise a TypeError on line XXX: No JSON representation for this object!

UPCOMING SUBPROJECTS :

Below a list of sub project that will developed.

1. SAJ

SAJ means Simple API for JSON. It’s a simple library that would parsing json stream how SAX parser does with XML. It’s parse a json stream and throws events that a callback class can receive.

2. JSON-VL

JSON-VL (JSON-ValidationLanguage) is a validation system used to validate a json stream. It can be extend to code generation and other functionality using the annotation attribute.

3. DOMJson

DOMJson is a DOM parser for json stream. It uses SAJ to parse json stream and makes a document model that represents the json.

4. JsonRPC

Json rpc is a system to execute remote procedure using json. It uses the SMD to describe services.

4. FactoryFinder

This library is a simple utility used by SAJ and DOMJson to manage the retrieve of factories.

Reading a JSON File :

make sure all dependency JAR  files are included :

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("j.txt");
    String jsonTxt = IOUtils.toString( is );

    JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );

    JSONObject routes = json.getJSONObject("routes");
    JSONObject legs = routes.getJSONObject("legs");
    JSONObject distance = legs.getJSONObject("distance");
    String dist = distance.getString("text");
}

You Might Also Like

  • Javabee

    Thanks for sharing Nice Article.

    • http://javamazon.com Groupusers

      Thanks