[Java Io]06_json operation

Source: Internet
Author: User

6.1 JSONKnowledge Background6.1.1 JSONIntroduction

Json:javascript Object Notation (JavaScript Object Notation )

JSON is the syntax for storing and exchanging textual information. Similar to XML.

JSON is smaller, faster, and easier to parse than XML.

JSON uses Javascript syntax to describe data objects, but JSON is still independent of language and platform. the JSON Parser and the JSON library support many different programming languages. JSON is supported by a very great number of dynamic (PHP,JSP,. NET) programming languages .

6.1.2 JSONGrammarJsonGrammar Rules
    • data in the name / value pairs in
    • Data is separated by commas
    • Curly braces Save Object
    • Square brackets Save Array


Jsonname/value pairs

the writing format for JSON data is: Name / value pairs.

name / value pairs include the field name (in double quotation marks), followed by a colon, and then the value:

"Name": "Zhangsan"

This is easy to understand, equivalent to this one JavaScript Statements:

Name = "Zhangsan"

Jsonvalue type

the JSON value can be:

    • String (in double quotes)
    • Number (integer or floating point)
    • Object (in curly braces)
    • Array (in square brackets)
    • Logical Value ( true or false )
    • Null

Data type graph for values:



6.1.3 JSONthe data structure

JSON has two kinds of data structures: objects and arrays.

JsonObject

An object is an unordered collection of "name / value pairs" (a collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), dictionary (dictionary), hash table (hash Table), a list of keys (keyed list), or an associative array (associative array).

an object with " {"(opening parenthesis) starts,"} "(closing parenthesis) ends. Each "name"is followed by a ":" (colon); "' Name / value ' pair 'is separated by","(comma).

the JSON object is written in curly braces:

{"Name": "Zhangsan", "Age": 24}

This is also easy to understand, with this article JavaScript statements are equivalent:

Name = "Zhangsan"

Age = 24

The reference figure is as follows:



JsonArray

the ordered list of values ( An ordered list of values). In most languages, it is understood as an array (array).

the JSON array is written in square brackets:

An array can contain multiple objects:

{

"People": [

{"Namr": "Zhangsan", "Age": 22},

{"Name": "Lisi", "Age": 24},

{"Name": "Wangwu", "Age": 27}

]

}

in the example above, the object "Employees" is an array that contains three objects. Each object represents a record of a person (with a first and last name).

The reference figure is as follows:



6.2 Javain ActionJsonData

There are many online JAVA type of Operation JSON of the Jar package, here is an introduction to one of the most common Jar Package: Json-lib .

6.2.1 JarPackage Download

:http://json-lib.sourceforge.net/

6.2.2 Basic MethodsNormal data transferJsonobject/**
* Normal data transfer JSON
*/
PublicStaticvoidJsonTest1 () {
Jsonobject Normaljson =NewJsonobject ();
Normaljson.put ("name", "Zhangsan");
Normaljson.put ("Sex", "male");
Normaljson.put ("Age",NewInteger (22));
System.out.println (Normaljson.tostring ());
}

MapData TransferJson/**
* Map data transfer JSON
*/
PublicStaticvoidJsonTest2 () {
map<string, object> map =NewHashmap<string, object> ();
Map.put ("name", "Zhangsan");//String Type
Map.put ("Age",NewInteger (22));//Number type
Object obj =NewString ("params");
Map.put ("obj", obj);//Object Type
Map.put ("Array",NewString[] {"A", "B", "C"});//Array Type
Map.put ("B_true", boolean.true);//Boolean type
Map.put ("B_false", Boolean.false);//Boolean type

Jsonobject JSON = jsonobject.fromobject (map);
System.out.println (Json.tostring ());
}


ListData TransferJson/**
* List data transfer JSON
*/
PublicStaticvoidJsonTest3 () {
list<string> list =NewArraylist<string> ();
List.add ("first");
List.add ("second");
Jsonarray Jsonarray = jsonarray.fromobject (list);
System.out.print (Jsonarray);
}


Array Data transferJson/**
* Array Data transfer JSON
*/
PublicStaticvoidJsonTest4 () {
string[] Colors =Newstring[]{"Red", "yellow", "Blue"};
Jsonarray Jsonarray = jsonarray.fromobject (colors);
System.out.println (Jsonarray);
}


BuildJsontext

Jsonstringer can be used to quickly construct a JSON -formatted text and convert it into a Stringthat can be written to a file;

Jsonstringer is a subclass of Jsonwriter;

Jsonstringer is generally constructed by using object (). Key (). Value (). Key (). Value (). EndObject ();

object () indicates the beginning of an object, that is, adding ' {';

EndObject () indicates the end of an object, that is, adding '} ';

Array () indicates the beginning of an array , that is, adding a ' [';

Endarray () indicates the end of an array, that is, adding a '] ';

key () means to add a key;

value () represents the addition of a value;

/**
* Build JSON text
*/
PublicStaticvoidJsonTest5 () {
Jsonstringer Stringer =NewJsonstringer ();
String str = Stringer.object (). Key ("Product"). Value ("Phone"). Key ("num"). Value (). EndObject (). toString ();
System.out.println (str);
}


References

http://www.json.org/

http://www.runoob.com/json/json-tutorial.html

[Java Io]06_json operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.