Json
JSON stands for JavaScript Object Notation. It is a lightweight text-based interchange format which is language indepent.
It looks like next :
{
"book": [
{
"id":1,
"language": null,
"edition": true,
"author": {"name":"abc"}
},
{
"id":7,
"language": "C++",
"edition": false,
"author": {"name":"def"}
}
]
}
JSON is key-value pair, key has to be string, while value has following types:
type | remark |
---|---|
Number | double precision floating point |
String | |
Boolean | true or false |
Array | it begins with [ and ends with ] |
Object | it begins with { and ends with } |
null |
In JavaScript, we can create json object like
var books = {
"Pascal" : [
{ "Name" : "Pascal Made Simple", "price" : 700 },
{ "Name" : "Guide to Pascal", "price" : 400 }
],
"Scala" : [
{ "Name" : "Scala for the Impatient", "price" : 1000 },
{ "Name" : "Scala in Depth", "price" : 1300 }
]
}
for(i = 0;i<books.Pascal.length;i++){
books.Pascal[i].Name+"</td></tr>");
}
And there are JSON.parse()/JSON.stringify() to convert json from/to js object.
If we want to merge two json, then we can use jQuery extend like jQuery.extend(true, json, newJson)
,
this will merge newJson into json.
In java, there are some 3rd library to do convertion, e.g. Gson, Jackson.
For Gson, its code as follows
Gson gson = new Gson();
JsonObject json = gson.fromJson(gson.toJson(c), JsonObject.class);
System.out.println(json.get("name").getAsString());
Written on July 20, 2017