Getting Started with JSON

Source: Internet
Author: User

First, Introduction

1. Description 1) JavaScript Object notation (JavaScript Notation)

2) The syntax for storing and exchanging text information. Like XML

3) Smaller, faster and easier to parse than XML

4) Independent of language

2. Grammar

1) Brace Save object, square brackets save array, data in name/value pairs, data separated by commas

For example:

{

"Employees": [

{"FirstName": "John", "LastName": "Doe"},

{"FirstName": "Anna", "LastName": "Smith"},

{"FirstName": "Peter", "LastName": "Jones"}

]

}

2)The JSON value can be:

      • Number (integer or floating point)
      • String (in double quotes)
      • Logical value (TRUE or FALSE)
      • Array (in square brackets)
      • Object (in curly braces)
      • Null
3. JSON Object1) The JSON object is written in curly braces:An object can contain multiple name/value pairs:{"FirstName": "John", "LastName": "Doe"}2) This is also easy to understand and is equivalent to this JavaScript statement:firstName = "John"lastName = "Doe"4. JSON array1) The JSON array is written in square brackets:An array can contain multiple objects:
[
{"FirstName": "John", "LastName": "Doe"},
{"FirstName": "Anna", "LastName": "Smith"},
{"FirstName": "Peter", "LastName": "Jones"}
]
Ii. parsing json in JavaScript1. JSON string' content '2. JSON string->javascript object1) eval functionvar obj = eval ("(" + txt + ")");Note: There are potential security issues2) JSON parservar obj = json.parse (TXT) third, in Java1, Jsonobject- jsonobject. Fromobject (Objectobject) JsonobjectThe object can be a Map,bean,json string ("{' Hello ': ' World ', ' abc ': ' XYZ '}")-getString (string key) string//Gets the property value of the object, similar to: Get (String key) Object,get (object key) object-p UT (object key,object value)//Increase the object's properties-Jsonobject.tobean (Jsonobject,class) object//map, you can directly force type conversions2, Jsonarray1) Turn Jsonarray- Jsonarray.fromobject (Object object) jsonarray //object can be a list, an array, a Map,json string (" [' name ', ' World ']')-Add (Object value)-Add (int index,object value)2) Turn List,map, arraydirect coercion of type conversions

-------------------------------------Split Line---------------------------------------------------

Here are a few specific examples to understand what is written above

1, Jsonobject

Define a JavaBean

 Public classStudent {PrivateString name; Private intGrade; PrivateList<string>teachers;  PublicStudent () {} PublicStudent (String name,intGrade, list<string>teachers) {        Super();  This. Name =name; Grade=grade;  This. Teachers =teachers; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetgrade () {returnGrade; }     Public voidSetgrade (intgrade) {Grade=grade; }     PublicList<string>getteachers () {returnteachers; }     Public voidSetteachers (list<string>teachers) {         This. Teachers =teachers; }}

Main method:

 Public classTestjson { Public Static voidMain (string[] args) {List<String> teacherlist =NewArraylist<string>(); Teacherlist.add ("Math"); Teacherlist.add ("中文版"); Student Stu=NewStudent ("Yaohuix", 2, teacherlist); Jsonobject Jsonobject=Jsonobject.fromobject (STU); //Bean to Jsonobject or JSON string (as long as ToString ())System.out.println (Jsonobject);//output: {"Grade": "Name": "Yaohuix", "Teachers": ["Math", "中文版"]}//Jsonobject or JSON string (as long as ToString ()) goes to BeanStudent Otherstu =(Student) Jsonobject.tobean (Jsonobject, Student.class); System.out.println (Otherstu.getname ());//Output: YaohuixSystem.out.println (Otherstu.getgrade ());//Output: 2System.out.println (Otherstu.getteachers ());//output: [Math, 中文版]    }}

Getting Started with JSON

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.