JSON (2): JSON processing on the server and client, and json processing on the server
This section describes how to use JSON to represent data. Next, we also need to solve how to generate JSON-format data on the server to send data to the client, and how to use JavaScript to process JSON-format data on the client.
Let's take a look at how the server outputs data in JSON format.
Taking Java as an example, we will demonstrate how to encode a Java object as a JSON text. When you encode a String object in JSON format, you only need to process special characters. In addition, the string must be (") rather:
<span style="font-family:SimHei;font-size:18px;">static Stringstring2Json(String s) { StringBuilder sb = newStringBuilder(s.length()+20); sb.append('\"'); for (int i=0; i<s.length(); i++) { char c = s.charAt(i); switch (c) { case '\"': sb.append("\\\""); break; case '\\': sb.append("\\\\"); break; case '/': sb.append("\\/"); break; case '\b': sb.append("\\b"); break; case '\f': sb.append("\\f"); break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; default: sb.append(c); } } sb.append('\"'); return sb.toString(); }</span>
It is much easier to express Number as JSON. Using Java polymorphism, we can handle multiple Number formats such as Integer, Long, and Float:
<span style="font-family:SimHei;font-size:18px;">static Stringnumber2Json(Number number) { return number.toString(); }</span>
The Boolean type can also be directly represented in JSON using the toString () method:
<span style="font-family:SimHei;font-size:18px;"> static Stringboolean2Json(Boolean bool) { return bool.toString(); }</span>
To encode an array in JSON format, you can encode each element in a loop:
<Span style = "font-family: SimHei; font-size: 18px;"> static String array2Json (Object [] array) {if (array. length = 0) return "[]"; StringBuilder sb = newStringBuilder (array. length <4); sb. append ('['); for (Object o: array) {sb. append (toJson (o); sb. append (',');} // change the last added ',' to ']': sb. setCharAt (sb. length ()-1, ']'); return sb. toString () ;}</span>
Finally, we need to encode Map <String, Object> In JSON format, because the JavaScript Object actually corresponds to the Java Map <String, Object>. The method is as follows:
<Span style = "font-family: SimHei; font-size: 18px;"> static Stringmap2Json (Map <String, Object> map) {if (map. isEmpty () return "{}"; StringBuilder sb = newStringBuilder (map. size () <4); sb. append ('{'); Set <String> keys = map. keySet (); for (String key: keys) {Object value = map. get (key); sb. append ('\ "'); sb. append (key); sb. append ('\ "'); sb. append (':'); sb. append (toJson (value); sb. append (',');} // change the last ',' to '}': sb. setCharAt (sb. length ()-1, '}'); return sb. toString () ;}</span>
Now the server can output data of various data types in JSON format. How can the client accept and display the data? Next, let's take a look at how the client uses JavaScript to process data in JSON format. Assume that the JSON data returned by the server is as follows:
{"Name": "Michael", "address ":
{& Quot; city & quot;: & quot; Beijing & quot;, & quot; street & quot;: & quot; ChaoyangRoad & quot;, & quot; postcode & quot;: 100025}
}
Using a simple JavaScript method, we can see how the client expresses JSON data to users:
<span style="font-family:SimHei;font-size:18px;">function handleJson() { varj={"name":"Michael","address": {"city":"Beijing","street":" ChaoyangRoad ","postcode":100025} }; document.write(j.name); document.write(j.address.city); }</span>
You only need to assign a value to a JavaScript variable to use the variable immediately and update the information on the page. JSON is easy to use than XML to read various nodes from the DOM. All we need to do is send an Ajax request and assign the JSON data returned by the server to a variable. Many Ajax frameworks already include the ability to process JSON data, such as Prototype (a popular JavaScript Library: http://prototypejs.org) that provides the evalJSON () method, directly convert the JSON text returned by the server into a JavaScript variable:
<span style="font-family:SimHei;font-size:18px;">new Ajax.Request("http://url", { method: "get", onSuccess: function(transport) { var json = transport.responseText.evalJSON(); // TODO: document.write(json.xxx); } });</span>
How to generate JSON-format data on the server to send data to the client, and how the client uses JavaScript to process JSON-format data. Of course, this is only the most basic use. Many changes need to be made in the specific application to the project practice. Let's first learn.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.