JSON lightweight data exchange format
Compared with XML, JSON parses faster and has smaller documents.
JSON format
{Attribute name: attribute value, attribute name: attribute value,...}
The type of attribute name can be string, number, boolean, null, object, and the attribute name must be enclosed in double quotes. If the attribute value is a string, it must also be enclosed in double quotes.
JSON represents an array
Format: [value,value,value], where value can be a basic data type, or object type or array type
Array type[{"name":"yangjq","age":22}, {"name":"andreny","age":21} ];Object type["name":"andreny", "hobby":["sing","dance","eat"] ]
Convert
graph LRJSON string --> JavaScript object
1. Use JavaScript's native function: eval(), but this method is dangerous and you need to be careful when using it.
var str={"name":"yangjq","age":23};var obj=eval("("+str+")");alert(obj.name);2. Use the parse() method provided by the native object JSON. If this object is invalid, it means that your browser version is too low! ! !
var str={"name":"andreny","age":22};var obj=JSON.parse(str);alert(obj.name);3. Method to add strings using JSON.js file.
var str={"name":"Cute Erha","age":9};var obj=str.parseJSON();alert(obj.name);Convert JSON object to JSON string
graph LRJSON object --> JSON string
Using the API provided by JSON: JSONObject, method provided by JSONArray
//The first method Emp emp=new Emp("1","andreny","man");JSONObject obj=JSONObject.fromObject(emp);String jsonStr=obj.toString();//The second method var obj={"name":"yangjq","age":23};var str1=obj.toJSONString();//The third method var str2=obj.JSON.stringify();Caching issues involved in AJAX when sending requests
==Note==: When sending a request, the AJAX object provided by the IE browser will first check whether it has accessed the address. If it has accessed it, the browser will no longer send the request.
Mechanisms for different browsers to treat cache
Chrome continues to send requests
Firefox continues to send requests
IE browser no longer sends requests
How to prevent the browser from sending requests because of cache?
Add a random number after the request address
xhr.open('get','getNumber.do?+Math.random(),true);Send a post request
Some questions about synchronization
After sending a synchronization request, the browser must wait for the server's response to arrive before continuing to operate in the page. When AJAX sends a synchronization request, the browser will lock the current page.
Test code
package yangjq.test;import java.util.ArrayList;import java.util.List;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import yangjq.entity.Friend;public class JSONtest {public static void main(String[] args) {System.out.println("-------------------------");test1();test2();test3();test4();}// Convert java object to json string public static void test1(){Friend f=new Friend();f.setName("andreny");f.setAge(23);JSONObject obj=JSONObject.fromObject(f);System.out.println(obj.toString());}// Convert JSON collection to JSON string public static void test2(){List<Friend> fs=new ArrayList<Friend>(); for (int i=0;i<10;i++){Friend f=new Friend();f.setName("yangjq"+i);f.setAge(12+i);fs.add(f);}JSONArray ja=JSONArray.fromObject(fs);System.out.println("------------/n"+fs);System.out.println(ja.toString());}// JSON string is converted to java object public static void test3(){String jsonStr="{/"name/":/"andreny/",/"age/":22}";JSONObject obj=JSONObject.fromObject(jsonStr);Friend friend=(Friend) JSONObject.toBean(obj,Friend.class);System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ "{/"name/":/"andreny/",/"age/":23}]";JSONArray obj=JSONArray.fromObject(jsonStr);List<Friend> friends=(List<Friend>) JSONArray.toCollection(obj,Friend.class);System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Running results:
---------------{"age":23,"name":"andreny"}----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [name=yangjq8, age=20], Friend [name=yangjq9, age=21]][{"age":12,"name":"yangjq0"},{"age":13,"name":"yangjq1"},{"age":14,"name":"yangjq2"},{"age":15,"name":"yangjq3"},{"age":16,"name":"yangjq4"},{"age":17,"name":"yangjq5" },{"age":18,"name":"yangjq6"},{"age":19,"name":"yangjq7"},{"age":20,"name":"yangjq8"},{"age":21,"name":"yangjq9"}]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [name=andreny, age=22]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Summarize
The above is all about this article discussing JSON's data exchange, caching and synchronization issues. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!