java json object相互转换

pom依赖:

   org.codehaus.jackson
   jackson-mapper-asl
   1.9.11

package com.sf.loganalysis.storm;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

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

/**
 * Created by 01139939 on 2017/3/23.
 */
public class Test1 {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String configinfo = "{\"taskName\":\"system1\",\"separator\":\"&\", \"topic\":\"test3\",\"zkaddr\":\"10.202.8.122:2181\",\"fields\":{\"1\":{\"success\":[1,2,3],\"fail\":[2,3,4,5]},\"2\":{\"lock\":[1,2,3]}}}";
        mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
        mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);

        Person person = new Person("Unmi", new Address("JiangXi"));
        TaskConfig config=new TaskConfig();
        config.setTaskName("task1");
        config.setSeparator("$");
        Map,Map,List>> map=new HashMap,Map,List>>();
        Map,List> inner=new HashMap,List>();
        List list=new ArrayList();
        list.add(1);
        list.add(2);
        inner.put("success",list);
        map.put("1",inner);
        config.setFields(map);
        config.setTopic("topic1");
        config.setZkaddr("zk");

        String json = mapper.writeValueAsString(config);
        System.out.println(json);

        TaskConfig taskconfig=  mapper.readValue(configinfo, TaskConfig.class);
        System.out.println(taskconfig.getFields());

    }
}

 class TaskConfig{
        //任务名称
        String taskName;
        //分割字段
        String separator;
        //kafka中原始日志数据的topic
        String topic;
     String zkaddr ;
        //预警字段Map>{0{success:1,2,3},{error:2,3,4}},如果不要中计预警,则中级预警对应的数字设置为-1
     Map, Map, List>> fields;

     public String getTaskName() {
         return taskName;
     }

     public void setTaskName(String taskName) {
         this.taskName = taskName;
     }

     public String getSeparator() {
         return separator;
     }

     public void setSeparator(String separator) {
         this.separator = separator;
     }

     public String getTopic() {
         return topic;
     }

     public void setTopic(String topic) {
         this.topic = topic;
     }

     public String getZkaddr() {
         return zkaddr;
     }

     public void setZkaddr(String zkaddr) {
         this.zkaddr = zkaddr;
     }

     public Map, Map, List>> getFields() {
         return fields;
     }

     public void setFields(Map, Map, List>> fields) {
         this.fields = fields;
     }
 }

class Person {
    private String name;
    private Address address;

    public Person(String name, Address address){
        this.name = name;
        this.address = address;
    }
}

class Address {
    private String province;

    public Address(String province){
        this.province = province;
    }

    public String getProvince(){
        return this.province;
    }
}
 
  
gson:

    com.google.code.gson
    gson
    2.8.0

public class JsonTest {
    public static void main(String[] args){
        String str="{\"msgType\":1,\"systemCode\":\"SGS_OMS\",\"msgContent\":{\"clientCode\":\"SJ001\",\"orderNo\":\"508900009076\",\"operateTime\":\"2017-04-06 11:20:28\",\"serviceType\":\"SE0083\",\"deliveryType\":\"2\",\"expectTime\":\"2017-04-06 11:26:28\",\"expectGetTime\":\"2017-04-06 11:50:28\",\"orderMemo\":\"\",\"orderDetailsInfo\":{\"orderAmount\":0.0,\"payMethod\":\"1\",\"payStatus\":\"1\",\"currency\":\"CNY\",\"category\":\"3\",\"totalWeight\":20.0,\"totalQty\":8.0,\"totalVolume\":0.0},\"orderItemInfoList\":[{\"itemName\":\"大衣\",\"itemQty\":\"1\",\"itemPrice\":\"0.0\",\"currency\":\"CNY\",\"volume\":0.0,\"itemWeight\":\"0.0\",\"itemMemo\":\"\"},{\"itemName\":\"裤子\",\"itemQty\":\"2\",\"itemPrice\":\"0.0\",\"currency\":\"CNY\",\"volume\":0.0,\"itemWeight\":\"0.0\",\"itemMemo\":\"\"},{\"itemName\":\"棉被\",\"itemQty\":\"1\",\"itemPrice\":\"0.0\",\"currency\":\"CNY\",\"volume\":0.0,\"itemWeight\":\"0.0\",\"itemMemo\":\"\"},{\"itemName\":\"洗衣液\",\"itemQty\":\"3\",\"itemPrice\":\"0.0\",\"currency\":\"CNY\",\"volume\":0.0,\"itemWeight\":\"0.0\",\"itemMemo\":\"\"},{\"itemName\":\"大米\",\"itemQty\":\"1\",\"itemPrice\":\"0.0\",\"currency\":\"CNY\",\"volume\":0.0,\"itemWeight\":\"0.0\",\"itemMemo\":\"\"}],\"sendOrderInfo\":{\"sendStoreName\":\"即刻抢单--1\",\"sendStoreCode\":\"GN9200001\",\"sendAddress\":\"深圳市粤海街道金地国际公寓1\",\"cityCode\":\"A440300000\",\"sendLng\":113.951271,\"sendLat\":22.527733,\"sendStoreContacts\":\"李子\",\"sendStoreMobile\":\"15509814273\",\"areaCode\":\"A440305000\",\"outsidePickupCode\":\"DP001\",\"sendTradingAreaName\":\"\"},\"receiveOrderInfo\":{\"receiveAddress\":\"广东省深圳市留学生创业大厦11\",\"receiveLng\":0.0,\"receiveLat\":0.0,\"receiveContacts\":\"冉春莱\",\"receiveMobile\":\"17756021869\"},\"orderFeeInfoList\":[{\"feeTypeCode\":\"SGSBBIL003\",\"feeTypeName\":\"商家配送费\",\"feeValue\":20.0}],\"contractPrincipal\":1,\"expressBranch\":\"755A\",\"shopOrderNo\":\"GN92000011491448774854\",\"orderCategory\":0,\"orderSource\":\"4\",\"shopType\":\"1\"}}";
        String str1="{\"msgType\":1,\"systemCode\":\"SGS_OMS\",\"msgContent\":{\"wo\":\"ni\"}}";
        CommonBean acc =null;
        try {
            acc = new ObjectMapper().readValue(str1, CommonBean.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        CommonBean acc = gson.fromJson(str, CommonBean.class);
        System.out.println(acc.getMsgContent().size());
        System.out.println(acc.getMsgContent().get("clientCode"));
        System.out.println(acc.getMsgType());
        System.out.println(acc);

    }
}

 
  
public class CommonBean {
    public String msgType;   // 订单消息类型:1下单   2 改单  3 取消订单
    public String systemCode;// SGS_OMS //系统编码
    public Map,Object> msgContent; //json内容
//省略setter getter
}

 
 

你可能感兴趣的:(java)