FileDocCategorySizeDatePackage
JSONUtil.javaAPI DocExample3273Mon May 29 12:39:18 BST 2006com.oreilly.ajax

JSONUtil

public class JSONUtil extends Object

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringbuildJSON(java.util.HashMap map, java.lang.String title)

        StringBuffer returnJSON = new StringBuffer("\r\n{\"" + title + "\":{");
        String key = "";
        String value = "";
        // loop through all the map entries.
        Iterator it = map.entrySet().iterator();

        while (it.hasNext())
        {
            Map.Entry e = (Map.Entry) it.next();
            value = (String) e.getValue();
            key = (String) e.getKey();
            returnJSON.append("\r\n\"" + key + "\": \"" + value + "\",");
        }
        // remove the last comma
        int lastCharIndex = returnJSON.length();
        returnJSON.deleteCharAt(lastCharIndex - 1);
        returnJSON.append("\r\n}}");
        return returnJSON.toString();
    
public static java.lang.StringbuildJSONArray(java.util.List list, java.lang.String title)

        StringBuffer returnJSON = new StringBuffer("\r\n{\"" + title + "\": [");
        String key = "username";
        String value = "";
        // loop through all the map entries.
        Iterator it = list.iterator();

        while (it.hasNext())
        {
            value = (String) it.next();
            returnJSON.append("\r\n{\"" + key + "\": \"" + value + "\"},");
        }
        // remove the last comma
        int lastCharIndex = returnJSON.length();
        returnJSON.deleteCharAt(lastCharIndex - 1);
        returnJSON.append("\r\n]}");
        return returnJSON.toString();
    
public static java.lang.StringconvertMapToProducts(java.util.HashMap map, java.lang.String title)

    StringBuffer returnJSON = new StringBuffer("\r\n{\"" + title + "\":[ ");
    String key = "";
    String value = "";
    // loop through all the map entries.
    Iterator it = map.entrySet().iterator();
    Product product = null;
    String totalCostFormatted="";
    
    DecimalFormat nf = new DecimalFormat("###,###.##");
    float itemTotalCost = 0.0F;
    float totalCost = 0.0F;
    while (it.hasNext())
    {
        Map.Entry e = (Map.Entry) it.next();
        value = (String) e.getValue();  //Item quantity
        key = (String) e.getKey();      //Item id
        product = ProductManager.getProductById(key);
        if (value!=null) //if the count is null, we don't care (not in shopping cart)
        {
        int intValue = Integer.parseInt(value);
        itemTotalCost =  intValue * product.getPrice();
        totalCost += itemTotalCost;
      
        returnJSON.append("\r\n{\"product\":\"" + product.getProductName() + 
                "\",\"price\":\"" + nf.format(product.getPrice()) + 
                "\",\"filename\":\"" + product.getFilename() + 
                "\",\"total\":\"" +  nf.format(itemTotalCost) + 
                "\",\"description\":\"" + product.getDescription() + 
                "\", \"value\":\"" + value + "\"},");
        }
    }
    totalCostFormatted = nf.format(totalCost);
    
    returnJSON.append("\r\n{\"total\":\""+totalCostFormatted+"\"}");
    returnJSON.append("\r\n]}");
    
    return returnJSON.toString();