2013年10月11日星期五

How to generate json data look-up table of answers after

Need to generate the following json

​{
    "response": "cart",
  "cart":{
listmap        "cartitem": [                 //购物车项
        :{ ---product  
map-->                "product":{
                    "id":"1200001",              //商品ID
                    "name": "雅培金装",          //商品名称
                    "pic":"",                    //商品图片URL
                    "price":"89",                 //商品售价
                    "number": 0,                //商品库存数量,0为缺货或下架
                    "uplimit":"10"               //商品购买数量上限
listmap    ------->  "product_property":[          //属性
                           {
                               "key": "颜色",
                               "value": "红色",
                            },
                            {
                               "key": "大小",
                               "value": "M",
                                }
                        ],
                    "isgift":"false"                //是否赠品
         }
                 "prodNum":3               //商品数量
            },

            {
                "product":{
                    "id":"1200001",              //商品ID
                    "name": "雅培金装",          //商品名称
                    "pic":"",                    //商品图片URL
                    "price":"89",                 //商品售价
                    "number": 0,                //商品库存数量,0为缺货或下架
                    "uplimit":"10",               //商品购买数量上限
                    "product_property":[          //属性
                           {
                                "key": "颜色",
                                "value": "红色",
                            },
                            {
                                 "key": "大小",
                                 "value": "M",
                             }
                     ]
           "isgift":"true"                //是否赠品
                },
                "prodNum":1               //商品数量
             }
        ],
        "prom":[                 //享受促销信息
          "促销信息一",
          "促销信息二"
        ],
        //购物车总计
        "totalCount":"3",            //商品数量总计
        "totalPrice":"230",          //商品金额总计
        "totalPoint":"230"           //商品积分总计
    }
}



There cart this table
id priduct_id buycount userid
1 1000162 2 10000
2 1000161 1 10000
3 1000229 2 10000




------ Solution ------------------------------------ --------
landlord first to distinguish between the point where the error
survey data from the database is out of the question ?
is the data into the database when the object is a problem ?
is converted to json object when a problem ?

------ Solution ------------------------------------ --------
define a json structure and the same class . json inside Object type is also defined as a category .

obtain data, instantiate , and copy it. By JSONObject ( or Gson) conversion .
------ Solution ---------------------------------------- ----
with json conversion tool ,
json-lib-2.3-jdk15.jar ( individuals more commonly used )
jackson-all-1.7.6.jar

The listmap first step to remove the database into json,
then assembled response and cart that two fields on the line.
tools to help you done, what method calls Bale , how to use own Baidu friends.


------ Solution ------------------------------------ --------
json-lib.jar
List list = new ArrayList () ;/ / list query results can be
list.add () ......
JSONArray jsonArr = JSONArray.fromObject (list);
String jsonStr = jsonArr.toString ();
------ For reference only --------------------- ------------------
this is the database query ! ! ! ! ! ! ! ! ! always wrong seeking answers

package com.ithm21.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.ithm21.util.DataSourceManager;

public class CartDaoImpl {

private QueryRunner runner = new QueryRunner(
DataSourceManager.getDataSource());

/**
 * 获取购物车里面的数据
 * 
 * @param productId
 * @return
 */
public Map<String, Object> getCart(String productId) {

// 购物车
Map<String, Object> cart = new HashMap<String, Object>();


// 购物车条目
List<Map<String, Object>> cartitem = new ArrayList<Map<String, Object>>();

// 商品
Map<String, Object> product = new HashMap<String, Object>();

//多个商品
Map<String,Object> productList = new HashMap<String,Object>();

// 商品属性
List<Map<String, Object>> product_property = new ArrayList<Map<String, Object>>();

try {
// 先查出单个商品
String sql = "SELECT product.id,product.name,pic.pic,product.price,product.number,product.buyLimit FROM product,pic WHERE product.id=pic.product_id AND product.id = ? ";
product = runner.query(sql, new MapHandler(), productId);

//将单个商品添加进入商品集合
productList.put("productlist", product);

// 查出商品的属性,
sql = "SELECT property.key,property.value FROM property WHERE id IN(SELECT property_id FROM product_property_filter  WHERE product_id = ?)";
product_property = runner.query(sql, new MapListHandler(),
productId);

// 将属性加入商品里面去
product.put("product_property", product_property);

// 不是促销商品(这里的属性应当定义成变量)
product.put("isgift", "false");

// 将商品添加进入cartitem
cartitem.add(productList);

/**
 * "prom":[ //享受促销信息 "促销信息一", "促销信息二" ],
 */
List<String> prom = new ArrayList<String>();
prom.add("促销信息一");
prom.add("促销信息二");
// 将促销信息添加到购物车
cart.put("prom", prom);

// 购物车总计
// "totalCount":"3", //商品数量总计
// "totalPrice":"230", //商品金额总计
// "totalPoint":"230" //商品积分总计
int totalCount = cartitem.size();
int totalPrice =

(int) cart.put("totalCount", totalCount);
cart.put("cartitem", cartitem);
cart.put("response", "cart");

return cart;

} catch (Exception e) {
e.printStackTrace();
}

return null;
}
}

------ For reference only ----------------------------------- ----
can use the tools were finally assembled into json .

------ For reference only ---------------------------------- -----


survey data from the database to check out a problem or only check out one , because the definition of the map, find out if the defined listmap not meet the results .. < br> ------ For reference only ---------------------------------------


come listmap first remove the key database is only a listmap like, it is nested , nested me dizzy ,
--- --- For reference only ---------------------------------------
definition wrong,
should be defined as an object , and then query is List so many . .

没有评论:

发表评论