JSON stands for JavaScript Object Notation.it is  lightweight .It gives us text-based open standard designed for human-readable  collection of data that we can access in a really logical manner.

It is used primarily to transmit data between a server and web application, as an alternative to XML.

Although originally derived from the JavaScript scripting language, JSON is a language-independent data format, and code for parsing and generating JSON data is readily available in a large variety of programming languages.

 

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

example:

{“children”:[{“name”:”1″,”size”:0},{“name”:”2″,”size”:0},{“name”:”3″,”size”:0.4},{“name”:”4″,”size”:0.5882},{“name”:”5″,”size”:19.4}]}

Steps to create json in java.

Jar files required for json

1.commons-beanutils-1.8.1

2.commons-collections-3.2.1

3.commons-lang-2.5

4.commons-logging-1.1.1

5.ezmorph-1.0.6

6.java-json

7.json-rpc-1.0

Put all the above jar files in WEB-INF/lib folder

create a class to create convert data into json

package com.helical.efw.json;

import java.util.List;

import java.util.Map;

import net.sf.json.JSONObject;

public class ConvertJson {

 

public void CommonJson(Map<String, List<Buble>> commonList)

{

String jsonString;

//Suppose in commonList data comming in this format

//{children=[{name=1, size=0.0000}, {name=2, size=0.0000},

//{name=3, size=0.4000}, {name=4, size=0.5882}, {name=5,

//size=19.4000}]}

JSONObject json = new JSONObject();

json.accumulateAll(commonList);

jsonString=json.toString();

System.out.println(json.toString());

}

}

 

 

Public class JsonProducer{

public static void main(String[] args) throws IOException {

ConvertJson cjson=new ConvertJson();

cjson.CommonJson();

}

JSONObject is a class in which there are so many methods available.

like:

JSONObject.accumulateAll(Map) :-It Accumulate values under a key

JSONObject.getJSONObject(java.lang.String key ):-we can get  get the JSONObject value associated with a key.

Leave a Reply