Home  >  Article  >  Web Front-end  >  Javascript JSON operation entry example_json

Javascript JSON operation entry example_json

WBOY
WBOYOriginal
2016-05-16 18:29:121252browse

The rules of JSON are simple: an object is an unordered collection of "name/value" pairs. An object starts with "{" (left bracket) and ends with "}" (right bracket). Each "name" is followed by a ":" (colon); "name/value" pairs are separated by a "," (comma). For specific details, please refer to http://www.json.org/json-zh.html
This article has an introductory article. You can also refer to JSON Learning Complete Manual with Graphics
A simple example:
js code

Copy code The code is as follows :

function showJSON() {
var user =
{
"username":"andy",
"age":20,
"info ": { "tel": "123456", "cellphone": "98765"},
"address":
[
{"city":"beijing","postcode":"222333" },
{"city":"newyork","postcode":"555666"}
]
}
alert(user.username);
alert(user.age);
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);
}

This represents a user object with attributes such as username, age, info, address, etc.
You can also use JSON to simply modify the data, modify the above example
js code
Copy code code As follows:

function showJSON() {
var user =
{
"username":"andy",
"age":20,
" info": { "tel": "123456", "cellphone": "98765"},
"address":
[
{"city":"beijing","postcode":"222333 "},
{"city":"newyork","postcode":"555666"}
]
}
alert(user.username);
alert(user.age) ;
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);
user.username = "Tom";
alert(user.username);
}

JSON provides the json.js package, download it from http://www.json.org/json.js , introduce it and then simply use object.toJSONString() to convert it into JSON data.
js code
Copy code The code is as follows:

function showCar() {
var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");
alert(carr.toJSONString());
}
function Car(make, model , year, color) {
this.make = make;
this.model = model;
this.year = year;
this.color = color;
}

You can use eval to convert JSON characters to Object
js code
Copy code The code is as follows:

function myEval() {
var str = '{ "name": "Violet", "occupation": "character" }';
var obj = eval('(' str ')');
alert(obj.toJSONString());
}

or use parseJSON() method
js code
Copy code The code is as follows:

function myEval() {
var str = '{ "name": "Violet", "occupation": "character" }';
var obj = str.parseJSON();
alert(obj.toJSONString());
}

Use prototype below Write an ajax example of JSON.
First write a servlet (mine is servlet.ajax.JSONTest1.java) and write a sentence
java code
response.getWriter().print("{ "name": "Violet", " occupation": "character" }");
Write an ajax request in the page
js code
Copy code The code is as follows:

function sendRequest() {
var url = "/MyWebApp/JSONTest1";
var mailAjax = new Ajax.Request(
url,
{
method: 'get',
onComplete: jsonResponse
}
);
}
function jsonResponse(originalRequest) {
alert(originalRequest.responseText);
var myobj = originalRequest.responseText.parseJSON();
alert(myobj.name);
}

JSON methods are provided in prototype-1.5.1.js , String.evalJSON(), you can modify the above method without using json.js.
js code
Copy code code As follows:

function jsonResponse(originalRequest) {
alert(originalRequest.responseText);
var myobj = originalRequest.responseText.evalJSON(true);
alert(myobj.name);
}

JSON also provides the java jar package http://www.json.org/java/index.html The API is also very simple. Here is an example
Fill in the javascript Request parameters
js code
Copy code The code is as follows:

function sendRequest() {
var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");
var pars = "car=" carr.toJSONString();
var url = " /MyWebApp/JSONTest1";
var mailAjax = new Ajax.Request(
url,
{
method: 'get',
parameters: pars,
onComplete: jsonResponse
}
);
}

Using the JSON request string, you can simply generate JSONObject and parse it, modify the servlet to add JSON processing (you need to use json.jar)
java code
Copy code The code is as follows:

private void doService(HttpServletRequest request, HttpServletResponse response ) throws IOException {
String s3 = request.getParameter("car");
try {
JSONObject jsonObj = new JSONObject(s3);
System.out.println(jsonObj.getString(" model"));
System.out.println(jsonObj.getInt("year"));
} catch (JSONException e) {
e.printStackTrace();
}
response.getWriter().print("{ "name": "Violet", "occupation": "character" }");
}

You can also use JSONObject to generate a JSON string , modify the servlet
java code
Copy code The code is as follows:

private void doService( HttpServletRequest request, HttpServletResponse response) throws IOException {
String s3 = request.getParameter("car");
try {
JSONObject jsonObj = new JSONObject(s3);
System.out.println( jsonObj.getString("model"));
System.out.println(jsonObj.getInt("year"));
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject resultJSON = new JSONObject();
try {
resultJSON.append("name", "Violet")
.append("occupation", "developer")
.append("age", new Integer(22));
System.out.println(resultJSON.toString());
} catch (JSONException e) {
e.printStackTrace();
}
response.getWriter().print(resultJSON.toString());
}

js code
Copy code The code is as follows:

function jsonResponse(originalRequest) {
alert(originalRequest.responseText);
var myobj = originalRequest.responseText. evalJSON(true);
alert(myobj.name);
alert(myobj.age);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn