接受第三方接口的json数据,解析转换成Javabean

接受第三方接口返回的json数据,解析转换成Javabean 的方法。

通过 net.sf.json.JSONObject.toBean 方法转换,
ContractRoot root = (ContractRoot)net.sf.json.JSONObject.toBean(jsonObject, ContractRoot.class, classMap);

详细可参考下面例子

接受第三方接口返回的json数据,解析转换成Javabean

public class outbandUtils {
    /**
     * 接受第三方接口返回的json数据,解析转换成Javabean
     * add by linsd 20180413
     * @param jsonObject
     * @param className
     */
    public static void turnJsonTOBean(JSONObject jsonObject,String className){
        if(!jsonObject.equals(null)){
            Map classMap = new HashMap();
            classMap.put("orderItem", OrderItem.class);
            classMap.put("customer", Customer.class);
            classMap.put("accounts", Accounts.class);
            classMap.put("partyCerts", PartyCerts.class);
            classMap.put("ordProdInsts", OrdProdInsts.class);
            classMap.put("ordProdInstAttrs", OrdProdInstAttrs.class);
            classMap.put("ordServAcctRels", OrdServAcctRels.class);
            classMap.put("ordProdInstStates", OrdProdInstStates.class);
            classMap.put("mktResCardInsts", MktResCardInsts.class);
            classMap.put("mktResCardInstAttrs", MktResCardInstAttrs.class);
            classMap.put("ordProdInstRels", OrdProdInstRels.class);
            classMap.put("ordOfferInsts", OrdOfferInsts.class);
            classMap.put("ordOfferInstAttrs", OrdOfferInstAttrs.class);
            classMap.put("ordOfferProdInstRels", OrdOfferProdInstRels.class);

            //由小到大测试对应的结点是否可以成功转为 java bean
            /*if(className.equals("OrderItemContext")){
                OrderItemContext orderItemContext1 = (OrderItemContext)net.sf.json.JSONObject.toBean(jsonObject, OrderItemContext.class, classMap);
                OrderItem o = orderItemContext1.getOrderItem();
                System.out.println(o.getCustOrderId()+","+o.getOrderItemCd());
                List partyCerts = orderItemContext1.getPartyCerts();
                PartyCerts p = partyCerts.get(0);
                PartyCerts p1 = partyCerts.get(1);
                System.out.println(p.getCertAddr()+","+p.getCertNum());
                System.out.println(p1.getCertAddr()+","+p1.getCertNum());
            }
            if(className.equals("SvcConts")){
                classMap.put("customerOrder", CustomerOrder.class);
                classMap.put("orderItems", OrderItemContext.class);

                SvcCont svc = (SvcCont)net.sf.json.JSONObject.toBean(jsonObject, SvcCont.class, classMap);
                CustomerOrder custOrder = svc.getCustomerOrder();
                System.out.println(custOrder.getAcceptDate()+","+custOrder.getAcceptRegionId());

                List orderItems = svc.getOrderItems();
                OrderItemContext context = orderItems.get(0);
                Customer customer = context.getCustomer();
                System.out.println(customer.getCustAddr()+","+customer.getCustAreaGrade());

                List partyCerts = context.getPartyCerts();
                PartyCerts p = partyCerts.get(0);
                PartyCerts p1 = partyCerts.get(1);
                System.out.println(p.getCertAddr()+","+p.getCertNum());
                System.out.println(p1.getCertAddr()+","+p1.getCertNum());
            }*/

            if(className.equals("ContractRoot")){
                classMap.put("customerOrder", CustomerOrder.class);
                classMap.put("orderItems", OrderItemContext.class);

                classMap.put("svcCont", SvcCont.class);
                classMap.put("tcpCont", TcpCont.class);
                ContractRoot root = (ContractRoot)net.sf.json.JSONObject.toBean(jsonObject, ContractRoot.class, classMap);

                SvcCont svc = root.getSvcCont();
                CustomerOrder custOrder = svc.getCustomerOrder();
                System.out.println("CustomerOrder:"+custOrder.getAcceptDate()+","+custOrder.getAcceptRegionId());

                List orderItems = svc.getOrderItems();
                OrderItemContext context = orderItems.get(0);
                Customer customer = context.getCustomer();
                System.out.println("Customer:"+customer.getCustAddr()+","+customer.getCustAreaGrade());

                List partyCerts = context.getPartyCerts();
                PartyCerts p = partyCerts.get(0);
                PartyCerts p1 = partyCerts.get(1);
                System.out.println("PartyCerts1:"+p.getCertAddr()+","+p.getCertNum());
                System.out.println("PartyCerts2:"+p1.getCertAddr()+","+p1.getCertNum());

                TcpCont tcp = root.getTcpCont();
                System.out.println("TcpCont:"+tcp.getAppKey()+","+tcp.getCustomerOrderId());
            }
        }
    }

}

测试调用

public class Test {

    /**
     *  接受第三方接口返回的json数据,解析转换成Javabean  测试
     * @param args
     */

    public static void main(String[] args){
        OrderItem orderItem = new OrderItem();
        orderItem.setCustOrderId("001");
        orderItem.setOrderItemCd("1");
        orderItem.setOrderItemId("002");
        orderItem.setServiceOfferId("500040001");
        orderItem.setStatusDate("20180413");

        Customer cust = new Customer();
        cust.setCustAddr("CustAddr");
        cust.setCustAreaGrade("custAreaGrade");

        PartyCerts part = new PartyCerts();
        part.setCertAddr("certAddr");
        part.setCertNum("certNum");

        PartyCerts part1 = new PartyCerts();
        part1.setCertAddr("certAddr1");
        part1.setCertNum("certNum1");

        List partyCerts = new ArrayList();
        partyCerts.add(part);
        partyCerts.add(part1);

        OrderItemContext orderItemContext = new OrderItemContext();
        orderItemContext.setOrderItem(orderItem);
        orderItemContext.setCustomer(cust);
        orderItemContext.setPartyCerts(partyCerts);

        List Oc = new ArrayList();
        Oc.add(orderItemContext);

        CustomerOrder custorder = new CustomerOrder();
        custorder.setAcceptDate("acceptDate");
        custorder.setAcceptRegionId("acceptRegionId");

        SvcCont svc = new SvcCont();
        svc.setCustomerOrder(custorder);
        svc.setOrderItems(Oc);

        TcpCont tcp = new TcpCont();
        tcp.setAppKey("appKey");
        tcp.setCustomerOrderId("customerOrderId");

        ContractRoot root = new ContractRoot();
        root.setSvcCont(svc);
        root.setTcpCont(tcp);

        JSONObject jsonObject = null;
        jsonObject =  jsonObject.fromObject(root);

        System.out.println(jsonObject);

        outbandUtils utils = new outbandUtils();
        //如 json 数据为原始json 数据(第三方直接拿到的数据),可通过如下方法转为 JSONObject
        //net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonObject);
        utils.turnJsonTOBean(jsonObject,"ContractRoot");
    }
}

你可能感兴趣的:(java)