JSONJava Script Object Notation)-
(JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
• A collection of name/value pairs. In various languages, this is realizehttp://www.blogger.com/img/blank.gifd as an object, record, strut, 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.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.
instead create DOM structure of java script it is better option to create JSON of corresponding DOM . also we can send huge data from server to client as text
format. when this java script is loaded in browser it will create DOM structure
and you can access like object .
syntax-
How to create JSON Object-
"obj-name"
{
"member1":"value" ,
"member2":"value" ,
"member3":"value" ,
"member4":"value" ,
.
.
.
}
ex-
" student" :{
"name":"abc"
};
we can access above JSON in browser like object.
student.name
we can create array using subscript notation:
array
var array-name=
[
{
"member1":"value1",
"member1":"value2",
"member1":"value3",
"nested-obj":{
"nestedmember":"value1"
}
},
{
"member1":"value1",
"member1":"value2",
"member1":"value3",
} ,
.
.
.
];
we can access just like c++ array-
array-name[0].member1
http://www.blogger.com/img/blank.gif
we can convert XML to JSON using ThomsFrank.js
It convert xml to JSON using java script at client side but fail to convert big model xml file or for complicated xml tag.
This lib is design by ThomsFrank
XML to JSON.
we can convert java object to JSON using GSON(google-JSON).
Gson is a Java library that can be used to convert a Java object into its
JSON representation.
GSON jar is available in following link.
GSON jar for download
GSON is best option for convert java to JSON but the problem with GSON is GSON
cant handle circular dependencies.
ex-convert java to JSON
import com.google.gson.Gson;
public class Test
{
private int data1 = 100;
private String data2 = "hello";
public static void main(String[] args)
{
Test obj = new Test();
Gson gson = new Gson();
//convert java object to JSON format
String json = gson.toJson(obj);
System.out.println(json);
}
}
o/p
{"data1":100,"data2":"hello"}
ex-2
Var processSet =
[
{
"id_":"p10",
"name_":"T9",
"type_":"cso30:c:ProcessBiological",
“Connector”:[
{
“Id_”:”c1”,
“Name_”:”c1”,
“Type_”: “cso30:c:InputProcess”,
“refID_”:”p13”,
“connectorSimulation”:
{
“firing”:
{
“firingstyle_”:”csml-connectorFiringStyle:threshold”,
“value”:”1”
},
"connectorKinetic":{
"parameter":[
{
"key_":"custom",
"value_":"m9*0.5"
},
{
"key_":"language",
"value_":"simplemath"
}…*
]
}
}
"view":{
"position":{
"id_":"default",
"x_":"62.690360156260056",
"y_":"43.35009487762857"
},
"shape":{
"image":{
"resourceID_":"20004",
"height_":"1273.0",
"width_":"1861.0",
"outlinecolor_":"0 0 0 0"
}
}
}
}
],
"processSimulation":{
"priority":{
"value_":"0"
},
"firing":{
"firingStyle_":"csml-firingStyle:and",
"firingOnce_":"false",
"type_":"csml-variable:Boolean",
"value_":"true"
},
"delay":{
"delayStyle_":"nodelay",
" value_":"0.0",
"script":{
"language_":"simplemath",
"containt_":"0.0"
}
},
"processKinetic":{
"calcStyle_":"csml-calcStyle:speed",
" kineticStyle_":"csml-kineticStyle:custom",
"fast_":"false",
"parameter":[
{
"key_":"custom",
"value_":"m9*0.5"
},
{
"key_":"language",
"value_":"simplemath"
},
]
}
},
"view":{
"position":{
"id_":"default",
"x_":"309.0",
"y_":"823.0"
},
"shape":{
"toolspecificgraphics":{
"linewidth_":"1.0",
"linecolor_":"0 0 0 255",
“name_”:”default”,
“Points_”:” 1119.0377997582852 334.0 1119.2042013866803 279.5”
}
}
}
}…*
];
All entities and process property written in single file (ex modelname.js) and that javascript file is send to client side .
We can access JSON at client side like object.
Ex-
processSet[0].processSimulation.parameter[0].key;