Ajax (c)-json data format

Source: Internet
Author: User

Why, learn JSON?

Most of the message passing between heterogeneous systems is now in JSON format

For example, WebService is based on XML, because to follow a variety of constraints, so it is cumbersome to pass data, and in high concurrency, the delivery of data is very slow. And Ali's Dubbo Distributed Service Framework (used only on the Java platform) is very efficient.


JSON (Javascriptobject Notation) is a simple data format that is lighter than XML. JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits.

The rules of JSON are simple: an object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).

The rules are as follows:

1) The map is represented by a colon (":"). Name: Value

2) The data is separated by commas (","). Name 1: value 1, Name 2: Value 2

3) The set of Mappings (objects) are represented by curly braces ("{}"). {Name 1: value 1, Name 2: Value 2}

4) a set (array) of side data is expressed in square brackets ("[]").

[ {Name 1: value, Name 2: Value 2},{Name 1: value, Name 2: Value 2}]

5) The type that the element value can have: string, number, object, array, true,false, null

/** * value in JSON can be: *    numeric type, string type, json,[],function */var json1 = {};var Json2 = {//key:valuename: ' aaa ', Id:1,aaa:fun Ction () {alert ("ADSF");}}; var Json3 = {//key:value (value is also a JSON) Person:{id:1,name: ' AA '}};//json array var json4 = [{id:1,name: ' aaa '},{id:2,name: ' BBB '}];var json5 = {setperson:function () {//key:value (a function)}};var json6 = {//key:json reference aaa:json4};/** * Traverse JSON */for (var I in Json2) {/** * I represents the key value */alert (i);/** * Json2[i] stands for value */if (typeof json2[i] = "function") {//if value is a functionjson2 I ();//Call the method}else{alert (Json2[i]);}} Find a form of key,value in JSON alert ("-------------" +json2["name"]);//Use this method to give a JSON dynamic add  key:valuejson2[' asdf '] = 1;alert (json2[' asdf ');/** * So the JSON is very flexible, so long as it can be parsed, it can pass */

Parsing JSON

JSON is just a text string. It is stored in the ResponseText property

In order to read the JSON data stored in the ResponseText attribute, you need to base the Eval statement on JavaScript. The function eval treats a string as its argument. The string is then executed as JavaScript code. Because the JSON string is made up of JavaScript code, it's inherently executable.

JSON Benefits:

As a data transfer format, JSON is similar to XML, but it is more dexterous.

JSON does not need to send header information that contains a specific content type from the server side.

Disadvantages:

The grammar is too rigorous

Code is hard to read

Conversion between JSON objects and Java objects (two ways)

First, Jackson

Jackson can easily convert Java objects to JSON objects and XML documents, as well as to convert JSON, XML

public class Jackjsontest {/** * Bean conversion JSON String * @throws Exception */@Testpublic void Testbean2json () throws Exception {User user = new user (); User.setdescription ("Desc1"); User.setid (1L); User.setname ("name1"); Objectmapper mapper = new Objectmapper (); String jsonstr = mapper.writevalueasstring (user); System.out.println (JSONSTR);//writevalue and writeobject have the same function mapper.writevalue (system.out, user);}}
To import the required

Jackson-annotations-2.4.0.jar
Jackson-core-2.4.5.jar
Jackson-databind-2.4.5.jar

Second, if you want to convert arrays, objects, maps, and lists into JSON data, then we need some jar packages:

json-lib-2.4-jdk15.jar
Ezmorph-1.0.6.jar
Commons-logging.jar
Commons-lang.jar
Commons-collections.jar
Commons-beanutils.jar

@Testpublic void Test2 () {//Converts an array to json:string[] arr = {"ASD", "DFGD", "ASD", "234"}; Jsonarray Jsonarray = Jsonarray.fromobject (arr); System.out.println (Jsonarray);//object converted to Json:user user = new User (1001, "Zhang San"); Jsonarray Jsonarray = jsonarray.fromobject (user);  System.out.println (Jsonarray);  To convert the MAP to JSON, use the Jsonobject object: map<string, object> map = new hashmap<string, object> (), Map.put ("id", 1001) ; Map.put ("UserName", "Zhang San"); Jsonobject jsonobject = jsonobject.fromobject (map); System.out.println (jsonobject);//Convert list to JSON data:list<user> list = new arraylist<user> (); User user = new User (1001, "Zhang San"), List.add (user), List.add (user), List.add (user); Jsonarray Jsonarray = jsonarray.fromobject (list); System.out.println (Jsonarray);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ajax (c)-json data format

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.