java json object 转换


   org.codehaus.jackson
   jackson-mapper-asl
   1.9.11

package com.xx

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;

/**
 * 
 */
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;
    }
}


你可能感兴趣的:(java json object 转换)