JSON data format
JSON, like XML, is based on a plain text data format. Because JSON is built for JavaScript, the data format is very simple. You can use JSON to transfer a simple String, Number, or Boolean, as well as an array or a complex Object.
String, Number, and Boolean are very simple to represent in JSON.
For example, JSON represents a simple String “ABC” in the format:
"abc"
Copy the code
Unicode characters can be output directly, except for the characters “, \, / and some control characters (\b, \f, \n, \r, \t) that need encoding. Here is a complete representation of a String:
- The complete representation structure of String
- The complete representation structure of String
A Number can be expressed as an integer or floating-point Number as follows:
- Representation structure of Number
- Representation structure of Number
This is consistent with the representation of most programming languages, for example:
12345 (integer) -3.9e10 (floating point)
Boolean type is expressed as true or false. In addition, null is represented as null in JavaScript. Note that true, false, and NULL are not quoted; otherwise, they would be treated as a String.
JSON can also represent an array object, using [] to contain all elements separated by commas. The elements can be any Value. For example, the following array contains a String, Number, Boolean, and null:
["abc".12345.false.null]
Copy the code
Object is represented in JSON as {} containing a series of unordered key-value pairs. Object is actually equivalent to Map<String, Object> in Java, not Java Class. Note that Key can only be a String.
For example, an Address object contains the following key-value:
city:Beijing
street:Chaoyang Road
postcode:100025(integer)Copy the code
Expressed in JSON as follows:
{"city":"Beijing"."street":" Chaoyang Road "."postcode":100025}
Copy the code
Value can also be another Object or an array, so complex objects can be nested. For example, a Person Object containing name and address objects can be represented as follows:
{"name":"Michael"."address":
{"city":"Beijing"."street":" Chaoyang Road "."postcode":100025}}Copy the code
JavaScript processes JSON data
Now that you’ve seen how to represent data in JSON, let’s figure out how to generate JSON-formatted data on the server for sending to the client, and how the client can process jSON-formatted data using JavaScript.
Let’s start by discussing how to process JSON data in JavaScript on a Web page. We can see how the client represents JSON data to the user using a simple JavaScript method:
function handleJson(a) {
var j={"name":"Michael"."address":
{"city":"Beijing"."street":" Chaoyang Road "."postcode":100025}}; document.write(j.name); document.write(j.address.city); }Copy the code
Suppose the server returns JSON data as above:
{"name":"Michael"."address":
{"city":"Beijing"."street":" Chaoyang Road "."postcode":100025}}Copy the code
You simply assign it to a JavaScript variable, and you can immediately use that variable and update the information in the page, which is much easier to use than XML, which requires reading various nodes from the DOM. All you need to do is send an Ajax request and assign the JSON data returned by the server to a variable. A number of Ajax frameworks have long included the ability to handle JSON data, such as Prototype (a popular JavaScript library: Prototypejs.org) provides the evalJSON() method, which directly converts the JSON text returned by the server into a JavaScript variable:
new Ajax.Request("http://url", {
method: "get",
onSuccess: function(transport) {
var json = transport.responseText.evalJSON();
// TODO: document.write(json.xxx);}});Copy the code
The server outputs data in JSON format
Let’s discuss how to output JSON data on the server side. Using Java as an example, we will demonstrate encoding a Java object as jSON-formatted text.
When encoding a String as JSON, you only need to handle the special characters. Also, strings must be represented with (“) instead of (‘) :
static String string2Json(String s) {
StringBuilder sb = new StringBuilder(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();
}
Copy the code
It is much easier to represent Number as JSON. Using Java’s polymorphism, we can handle multiple Number formats such as Integer, Long, Float, and so on:
static String number2Json(Number number) {
return number.toString();
}
Copy the code
Boolean types can also be represented directly by the toString() method:
static String boolean2Json(Boolean bool) {
return bool.toString();
}
Copy the code
To encode an array in JSON format, we loop through each element:
static String array2Json(Object[] array) {
if (array.length==0)
return "[]";
StringBuilder sb = new StringBuilder(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();
}
Copy the code
Finally, we need to encode Map<String, Object> in JSON format, because JavaScript Object actually corresponds to Java Map<String, Object>. The method is as follows:
static String map2Json(Map<String, Object> map) {
if (map.isEmpty())
return "{}";
StringBuilder sb = new StringBuilder(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();
}
Copy the code
To work uniformly with arbitrary Java objects, we write an entry method toJson(Object) that can encode arbitrary Java objects in JSON format:
public static String toJson(Object o) {
if (o==null)
return "null";
if (o instanceof String)
return string2Json((String)o);
if (o instanceof Boolean)
return boolean2Json((Boolean)o);
if (o instanceof Number)
return number2Json((Number)o);
if (o instanceof Map)
return map2Json((Map<String, Object>)o);
if (o instanceof Object[])
return array2Json((Object[])o);
throw new RuntimeException("Unsupported type: " + o.getClass().getName());
}
Copy the code
Java objects are not rigorously checked. Unsupported objects (such as lists) will throw runtimeExceptions directly. In addition, to ensure that the output JSON is valid, the Key of the Map<String, Object> Object cannot contain special characters. The observant reader may also notice that objects referenced in a loop can cause infinite recursion, for example, StackOverflowException can be detected by carefully constructing a Map of loop references:
@Test(expected=StackOverflowError.class)
public void testRecurrsiveMap2Json(a) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("key", map);
JsonUtil.map2Json(map);
}
Copy the code
Fortunately, the JSON data processed by the server should eventually be converted to simple JavaScript objects, so recursive references are unlikely.
Finally, when exporting JSON through a Servlet or MVC framework, you need to set the correct MIME type (Application/JSON) and character encoding. Assuming that the server uses UTF-8 encoding, you can output the encoded JSON text with the following code:
response.setContentType("application/json; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.write(JsonUtil.toJson(obj));
pw.flush();
Copy the code
summary
JSON is already part of the JavaScript standard. Currently, the major browsers have excellent JSON support. With JSON, you can get away from parsing XML, and JSON is currently the most flexible lightweight solution for Web 2.0 sites that use Ajax.