Creation JSON from Java-classes
Merits and demerits which can be found in other ways of creation XML, can appear correct for JSON. Probably, there is one more case of use of technologies of a pattern of performance. However, use JSON is logically closer to use of serialization of the applications given for lines, than to creation of performance of a status of the application. I am going to show you how to use org.json and toJSONObject () at creation toJSONObject () methods in your Java-classes. You JSONObject can be easy also serializovan in JSON. Listing 8 reminds our discussion about XML, showing toJSONObject () realization for Order a class.
Listing 8. toJSONObject () realization of a method for class Order
public JSONObject toJSONObject () {
JSONObject json = new JSONObject ();
json.put ("id", id);
json.put ("cost", getFormattedCost ());
json.put ("date", date);
JSONArray jsonItems = new JSONArray ();
for (Iterator <Item> iter =
items.iterator (); iter.hasNext ();) {
jsonItems.put (iter.next () .toJSONObject ());
}
json.put ("items", jsonItems);
return json;
}
As you can see, the API-interface org.json is very simple. JSONObject represents JavaScript-object (a communicative file) and carries out various put () methods which use String a key and value String, or another JSON type. JSONArray represents not numbered file, therefore it{him} put () the method requests only one value. Remember, that listing 8, alternative to creation jsonItems a file and the subsequent to this his{its} fastening for json object with put () a method, should cause json.accumulate ("items", iter.next () .toJSONObject ()); for each thing. accumulate () the method is similar put () except that he adheres value to the conducted the indexation file determined by a key.
Listing 9 shows, how serializovat` JSONObject and to write it{him} according to search servleta.
Listing 9. Generation serializovannogo JSON search from JSONObject
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws java.io. IOException, ServletException {
String custId = req.getParameter ("username");
Customer customer = getCustomer (custId);
res.setContentType ("application/x-json");
res.getWriter () .print (customer.toJSONObject ());
}
toString () the method in JSONObject, called here, performs all job, therefore on your share does not remain, practically, anything. Remember, that the content type application/x-json is a little bit doubtful, during a spelling of these lines we did not come to a consensus to what MIME type JSON of it{him} should attribute{relate}. However, application/x-json - a reasonable choice. Listing 10 shows search of an example from this code servleta.
Listing 10. JSON-performance Customer-bean
{
"orders": [
{
"items": [
{
"price": " $49.99",
"description": " 512 Megabyte Type 1 CompactFlash card.
Manufactured by Oolong Industries ",
"name": " Oolong 512MB CF Card ",
"id": "i-55768"
},
{
"price": " $299.99",
"description": " 7.2 Megapixel digital camera featuring six
shooting modes and 3x optical zoom. Silver. ",
"name": " Fujak Superpix72 Camera ",
"id": "i-74491"
}
],
"date": "08-26-2005",
"cost": " $349.98",
"id": "o-11123"
}
],
"realname": " James Hyrax ",
"username": "jimmy66"
}

|