1. When JS reads JSON and writes AJAX, it is often necessary to parse a string returned from the server. Here we will briefly introduce the two formats of the characters returned by the server and the parsing methods of JS for them. JSON is the js object tag (JavaScriptObjectNotation). It is a method for declaring objects in JS... SyntaxHighlighter.
1. JS read JSON
When writing AJAX, it is often necessary to parse a string returned from the server. Here we will briefly introduce two formats of characters returned by the server and the parsing methods of JS for them.
JSON is the JavaScript Object Tag (JavaScript Object Notation). It is a string that is combined by declaring objects in JS.
JS can define objects as follows:
Var obj =
{
Id: 2,
Name: 'n'
};
Alert (obj. name );
In this way, the object obj is defined. It has two common attributes: id and name. You can directly access its attribute values using obj. id.
When obtaining data from the server, there is usually more than one object, which requires an array of objects. The array of objects in JS can be defined using [], as shown below:
Var objs = [{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}];
Alert (objs [0]. id );
In this way, the object array objs is defined. It contains two objects and can be accessed using indexes. For example, objs [0] Will reference the first object. Here you may have thought about the format of the string returned by the server, but the string is a string after all, and we need to convert it to a variable that can be operated by JS. This uses the eval function. See the following example:
Var objs = eval ("[{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}]");
Alert (objs [0]. id); // return 1
All right, on the server side, you only need to use the format [{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}] returns a string. on the client side, you can use eval () to execute the returned string to obtain an array of objects. The following is a simple example using AJAX. The code for retrieving json with js is as follows:
Var objs = eval (request. responseText );
Alert (objs. length); // 2
Alert (objs [0]. id); // 1
Alert (objs [1]. name); // 'n' _ 2'
Add a test button to view the effect:
However, sometimes the Json data is spliced by itself, and is directly read using js, and it is found that the read data is undefined... that is to say, like
Var s = "{name1: 'name1', name2: 'name2'}"; js treats it as a string rather than JSON data. Therefore, we need to convert it into JSON data.
Note: If you write tmp = eval (tmp) at this time, the error "missing;" will be reported. It should be: tmp = eval ("(" + tmp + ")");
Then use document. getElementById ("txt1"). value = tmp. name1.
Another method for converting strings to JSON: // json. js is required.
Var obj = strJSON. parseJSON ();
Var obj = JSON. parse (strJSON );
Convert JSON to a string:
Var str = obj. toJSONString ();
Var str = JSON. stringify (obj)
2. Java reads JSON data:
Import java. io .*;
Import org. json .*;
Public class Demo {
Public static void main (String [] args) throws Exception {
String str = "{\" a \ ": \" B \ ", \" c \ ": \" d \"}";
JSONObject a = new JSONObject (str );
System. out. println (a); // {"c": "d", "a": "B "}
System. out. println (a. get ("c"); // d
}
}
Just give the string as a parameter to JSONObject ......
Excerpted from the column of wandering hacker