有时为了项目需求,会将对象数据转换成json数据,以下是个人根据项目需求实现的方法。

项目中需要将数据格式:

[{
    "node": "0",
    "index": null,
    "status": null,
    "preNode": null,
    "postNode": [{
        "node": "xxx_4"
    },
    {
        "node": "xxx_3"
    },
    {
        "node": "xxx_2"
    },
    {
        "node": "xxx_14"
    }]
},
{
    "node": "xxx_2",
    "index": "index_1",
    "status": "表达式1",
    "preNode": [{
        "node": "xxx_0"
    }],
    "postNode": [{
        "node": "xxx_5"
    }]
},
{
    "node": "xxx_14",
    "index": "index_4",
    "status": "表达式5",
    "preNode": [{
        "node": "xxx_0"
    }],
    "postNode": [{
        "node": "xxx_13"
    },
    {
        "node": "xxx_5"
    }]
},
{
    "node": "xxx_3",
    "index": "index_2",
    "status": "表达式2",
    "preNode": [{
        "node": "xxx_0"
    }],
    "postNode": [{
        "node": "xxx_5"
    }]
},
{
    "node": "xxx_4",
    "index": "index_3",
    "status": "表达式3",
    "preNode": [{
        "node": "xxx_0"
    }],
    "postNode": [{
        "node": "xxx_12"
    }]
},
{
    "node": "xxx_5",
    "index": "index_4",
    "status": "表达式4",
    "preNode": [{
        "node": "xxx_3"
    },
    {
        "node": "xxx_2"
    },
    {
        "node": "xxx_14"
    }],
    "postNode": [{
        "node": "xxx_6"
    }]
},
{
    "node": "xxx_6",
    "index": "index_5",
    "status": "表达式6",
    "preNode": [{
        "node": "xxx_5"
    }],
    "postNode": [{
        "node": "xxx_7"
    }]
},
{
    "node": "xxx_7",
    "index": "index_6",
    "status": "表达式7",
    "preNode": [{
        "node": "xxx_6"
    }],
    "postNode": [{
        "node": "xxx_8"
    }]
},
{
    "node": "xxx_8",
    "index": "index_4",
    "status": "表达式8",
    "preNode": [{
        "node": "xxx_7"
    }],
    "postNode": [{
        "node": "xxx_9"
    },
    {
        "node": "xxx_10"
    }]
},
{
    "node": "xxx_9",
    "index": "index_5",
    "status": "表达式5",
    "preNode": [{
        "node": "xxx_8"
    }],
    "postNode": [{
        "node": "xxx_11"
    }]
},
{
    "node": "xxx_10",
    "index": "index_7",
    "status": "表达式6",
    "preNode": [{
        "node": "xxx_8"
    }],
    "postNode": [{
        "node": "xxx_11"
    }]
},
{
    "node": "xxx_11",
    "index": "index_8",
    "status": "表达式7",
    "preNode": [{
        "node": "xxx_9"
    },
    {
        "node": "xxx_10"
    }],
    "postNode": [{
        "node": "xxx_12"
    }]
},
{
    "node": "xxx_12",
    "index": "index_8",
    "status": "表达式7",
    "preNode": [{
        "node": "xxx_11"
    },
    {
        "node": "xxx_4"
    }],
    "postNode": [{
        "node": "xxx_13"
    }]
},
{
    "node": "xxx_13",
    "index": "",
    "status": "",
    "preNode": [{
        "node": "xxx_14"
    },
    {
        "node": "xxx_12"
    }],
    "postNode": []
},
{
    "node": "9999",
    "index": null,
    "status": null,
    "preNode": [{
        "node": "xxx_14"
    },
    {
        "node": "xxx_12"
    }],
    "postNode": null
}]

项目中list对象内容

 JsonModel{node='xxx_2', preNode='', index='index_1', status='表达式1'}
 JsonModel{node='xxx_14', preNode='', index='index_4', status='表达式5'}
 JsonModel{node='xxx_3', preNode='', index='index_2', status='表达式2'}
 JsonModel{node='xxx_4', preNode='', index='index_3', status='表达式3'}
 JsonModel{node='xxx_5', preNode='xxx_2', index='index_4', status='表达式4'}
 JsonModel{node='xxx_5', preNode='xxx_3', index='index_4', status='表达式5'}
 JsonModel{node='xxx_5', preNode='xxx_14', index='index_4', status='表达式5'}
 JsonModel{node='xxx_6', preNode='xxx_5', index='index_5', status='表达式6'}
 JsonModel{node='xxx_7', preNode='xxx_6', index='index_6', status='表达式7'}
 JsonModel{node='xxx_8', preNode='xxx_7', index='index_4', status='表达式8'}
 JsonModel{node='xxx_9', preNode='xxx_8', index='index_5', status='表达式5'}
 JsonModel{node='xxx_10', preNode='xxx_8', index='index_7', status='表达式6'}
 JsonModel{node='xxx_11', preNode='xxx_10', index='index_8', status='表达式7'}
 JsonModel{node='xxx_11', preNode='xxx_9', index='index_8', status='表达式8'}
 JsonModel{node='xxx_12', preNode='xxx_11', index='index_8', status='表达式7'}
 JsonModel{node='xxx_12', preNode='xxx_4', index='index_8', status='表达式8'}
 JsonModel{node='xxx_13', preNode='xxx_14', index='', status='表达式13'}
 JsonModel{node='xxx_13', preNode='xxx_12', index='', status='表达式14'}
public void testJson() throws Exception{
        List<JsonModel> list=new ArrayList<>();
        ObjectMapper objectMapper=new ObjectMapper();

        JsonModel jsonModel=new JsonModel("xxx_2","","index_1","表达式1");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_14","","index_4","表达式5");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_3","","index_2","表达式2");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_4","","index_3","表达式3");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_5","xxx_2","index_4","表达式4");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_5","xxx_3","index_4","表达式5");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_5","xxx_14","index_4","表达式5");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_6","xxx_5","index_5","表达式6");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_7","xxx_6","index_6","表达式7");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_8","xxx_7","index_4","表达式8");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_9","xxx_8","index_5","表达式5");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_10","xxx_8","index_7","表达式6");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_11","xxx_10","index_8","表达式7");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_11","xxx_9","index_8","表达式8");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_12","xxx_11","index_8","表达式7");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_12","xxx_4","index_8","表达式8");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_13","xxx_14","","表达式13");
        list.add(jsonModel);

        jsonModel=new JsonModel("xxx_13","xxx_12","","表达式14");
        list.add(jsonModel);

        list.forEach(var->
                System.out.println(var.toString())
        );
        System.out.println();

        //将list转成String
        String str=objectMapper.writeValueAsString(list);

        //将字符串转成JsonNode
        JsonNode rootNode = objectMapper.readTree(str);

        Set<ChildNode> preNode_set=null;
        Set<ChildNode> postNode_set=null;

        List<Node> nodeList=new ArrayList<>();

        //寻找第一个indxe 创建一个start节点
        Node node=new Node();
        node.setNode("0");
        Set<ChildNode> childNodeList=new HashSet<>();
        for(int i=0;i<rootNode.size();i++){

            if("".equals(rootNode.get(i).get("preNode").asText())){
               childNodeList.add(new ChildNode(rootNode.get(i).get("node").asText()));
            }
        }
        node.setPostNode(childNodeList);
        nodeList.add(node);

        //这种方式在如果我们只需要一个长json串中某个字段值时非常方便
        for (int i=0;i<rootNode.size();){

            preNode_set=new HashSet<>();
            postNode_set=new HashSet<>();

            //直接从rootNode中获取某个键的值,
            JsonNode nameNode = (rootNode.get(i)).get("node");
            String name = nameNode.asText();

            String index=rootNode.get(i).get("index").asText();
            String status=rootNode.get(i).get("status").asText();

            //找出后置节点  post_node
            for(int j=i+1;j<rootNode.size();j++){
                String names = (rootNode.get(j)).get("preNode").asText();

                if(name.equals(names)){

                    //  if(!"".equals(rootNode.get(j).get("postNode").asText())) {
                    postNode_set.add(new ChildNode((rootNode.get(j)).get("node").asText()));
                    //}
                }
            }

            //找前置节点
            for(int j=0;j<list.size();j++){
                String names = (rootNode.get(j)).get("node").asText();

                //对于后一个的index相同时  需要跳过
                    if(name.equals(names)){

                        if(!"".equals(rootNode.get(j).get("preNode").asText())){
                            preNode_set.add(new ChildNode((rootNode.get(j)).get("preNode").asText()));
                            if(j>i){
                                ++i;
                            }else{
                                i++;
                            }
                        }else{
                            preNode_set.add(new ChildNode("0"));
                            i++;
                        }
                    }
            }

            Node nodes=new Node();

            nodes.setIndex(index);
            nodes.setNode(name);
            nodes.setStatus(status);
            nodes.setPostNode(postNode_set);
            nodes.setPreNode(preNode_set);

            nodeList.add(nodes);

        }

        //后置节点
        Node pre_node=new Node();
        pre_node.setNode("9999");
        Set<ChildNode> childNodes=new HashSet<>();
        for(int i=0;i<rootNode.size();i++){

            //最后一个end 节点的pre_node
            if("".equals(rootNode.get(i).get("index").asText())){

                childNodes.add(new ChildNode(rootNode.get(i).get("preNode").asText()));
            }
        }
        pre_node.setPreNode(childNodes);
        nodeList.add(pre_node);

        String s = objectMapper.writeValueAsString(nodeList);
        System.out.println(s);

        System.out.println();
        jsonTest(s);

    }

    /**
     * <p>json --> 对象</p>
     * @param s
     * @throws Exception
     */
    public void jsonTest(String s) throws Exception{
        List<Node> nodeList=new ArrayList<>();
        ObjectMapper objectMapper=new ObjectMapper();
      
  
     //将字符串用objectMapper转换成jsonNode JsonNode jsonNode=objectMapper.readTree(s); List<JsonModel> jsonModels=new ArrayList<>(); for(int i=1;i<jsonNode.size()-1;i++){ String index=jsonNode.get(i).get("index").asText(); String status=jsonNode.get(i).get("status").asText(); String node=jsonNode.get(i).get("node").asText(); for(int j=0;j<jsonNode.get(i).get("preNode").size();j++){ String childNode= jsonNode.get(i).get("preNode").get(j).get("node").asText(); JsonModel jsonModel=new JsonModel(node,childNode,index,status); jsonModels.add(jsonModel); } } jsonModels.forEach(val-> System.out.println(val.toString()) ); }

以下是所用到的对象实体类

 package com.yf.af.biz.test;

 /**
  * Created by chen on 2017/7/14.
  */
 public class JsonModel {
     private String node;
     private String preNode;
     private String index;
     private String status;

     public String getNode() {
         return node;
     }

     public void setNode(String node) {
         this.node = node;
     }

     public String getPreNode() {
         return preNode;
     }

     public void setPreNode(String preNode) {
         this.preNode = preNode;
     }

     public String getIndex() {
         return index;
     }

     public void setIndex(String index) {
         this.index = index;
     }

     public String getStatus() {
         return status;
     }

     public void setStatus(String status) {
         this.status = status;
     }

     public JsonModel(String node, String preNode, String index, String status) {
         this.node = node;
         this.preNode = preNode;
         this.index = index;
         this.status = status;
     }

     public JsonModel() {
     }

     @Override
     public String toString() {
         return "JsonModel{" +
                 "node='" + node + '\'' +
                 ", preNode='" + preNode + '\'' +
                 ", index='" + index + '\'' +
                 ", status='" + status + '\'' +
                 '}';
     }
 }
 package com.yf.af.biz.test;

 import java.util.List;
 import java.util.Set;

 /**
  * Created by chen on 2017/7/15.
  */
 public class Node {
     private String node;
     private String index;
     private String status;

     private Set<ChildNode> preNode;

     private Set<ChildNode> postNode;

     public String getNode() {
         return node;
     }

     public void setNode(String node) {
         this.node = node;
     }

     public String getIndex() {
         return index;
     }

     public void setIndex(String index) {
         this.index = index;
     }

     public String getStatus() {
         return status;
     }

     public void setStatus(String status) {
         this.status = status;
     }

     public Set<ChildNode> getPreNode() {
         return preNode;
     }

     public void setPreNode(Set<ChildNode> preNode) {
         this.preNode = preNode;
     }

     public Set<ChildNode> getPostNode() {
         return postNode;
     }

     public void setPostNode(Set<ChildNode> postNode) {
         this.postNode = postNode;
     }

     @Override
     public String toString() {
         return "Node{" +
                 "node='" + node + '\'' +
                 ", index='" + index + '\'' +
                 ", status='" + status + '\'' +
                 ", preNode=" + preNode +
                 ", postNode=" + postNode +
                 '}';
     }

     public Node() {
     }
 }
 package com.yf.af.biz.test;

 /**
  * Created by chen on 2017/7/15.
  */
 public class ChildNode {
     private String node;

     public String getNode() {
         return node;
     }

     public void setNode(String node) {
         this.node = node;
     }

     public ChildNode(String node) {
         this.node = node;
     }

     public ChildNode() {
     }
 }

java 对象与json互转的更多相关文章

  1. java对象与json互转

    package com.liveyc; import java.io.StringWriter; import org.junit.Test; import com.fasterxml.jackson ...

  2. java对象与json串互转

    1:java对象与json串转换: java对象—json串: JSONObject JSONStr = JSONObject.fromObject(object); String str = JSO ...

  3. Java对象、Json、Xml转换工具Jackson使用

    在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson.JSON-lib.Jackson等等.本文主要介绍Jackson的使用,Jacks ...

  4. json相关类库,java对象与json相互转换

    有效选择七个关于Java的JSON开源类库 转自:http://www.open-open.com/lib/view/open1397870197828.html 翻译: (英语原文:http://w ...

  5. java对象与json对象间的相互转换

    工程中所需的jar包,因为在网上不太好找,所以我将它放到我的网盘里了,如有需要随便下载. 点击下载 1.简单的解析json字符串 首先将json字符串转换为json对象,然后再解析json对象,过程如 ...

  6. JSON-lib框架,JAVA对象与JSON、XML之间的相互转换

    Json-lib可以将Java对象转成json格式的字符串,也可以将Java对象转换成xml格式的文档,同样可以将json字符串转换成Java对象或是将xml字符串转换成Java对象. 一. 准备工作 ...

  7. java对象转json应clone,避免生成json串有问题

    今天因为一个java对象转json,搞了我一下午,在些记录一下: 是这样:我在strtuts2的action中调用services返回 Row: 26, 中国银行海鹰, 29, 东楼, 36, 1F ...

  8. java对象和json对象之间互相转换

    import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.Li ...

  9. JackSon将java对象转换为JSON字符串

    JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的writeValueA ...

随机推荐

  1. 深度解析PHP数组函数array_combine

    前些天写了一篇关于array_merge的函数解析. 今天来看一个新的函数array_combine() 此函数一共有两个参数,一个是合并后数组的键名,另一个为键值. 注意:合并后数组的键名放在第一个 ...

  2. scroll抖动问题

    参考软文: http://www.cnblogs.com/coco1s/p/5499469.html function throttle(func, wait, mustRun) { var time ...

  3. javaSE_Java第一周总结:有难度题目集合

    第一周练习总结 说明:尽量采用多种做法解决 1.使用三种方法实现变量交换 public class Test1Change{ public static void main(String[] args ...

  4. springboot问题:解决异常Unable to start embedded container;

    使用eclipse创建springboot练习时,当主函数与控制器同时写在同一个类时,启动项目正常运行,而当把主函数单独放在一个类中时,无论是与控制器同包还是控制器所在的包是其子包,都报: org.s ...

  5. 【源码分享】jquery+css实现侧边导航栏

    jquery+css实现侧边导航栏 最近做项目的时候,突然想用一个侧边导航栏,网上找了几个插件,有的太丑而且不太符合我的预期.与其修改别人的代码,不如自己来写一个了.废话不多说先上图,感兴趣的请继续看 ...

  6. xssless - 自动化的XSS payload攻击器

    XSSLESS 一个用Python编写的自动化XSS 负载(payload)攻击器 用法: 记录请求 并结合Burp proxy 选择你想生成的请求,然后右键选择“保存项目” 使用xssless生成你 ...

  7. 微信小程序开发 -- 02

    微信小程序开发 --02 微信小程序在开发中,难度系数不是很大,其中应用的技术也是web开发中常用的技术,虽然在微信开发者工具中的叫法与常见的web开发的叫法不太一样. 首先,在微信小程序开发中,代码 ...

  8. ActiveMQ 学习第二弹

    经历了昨天的初识 ActiveMQ,正好今天下班有点事耽搁了还没法回家,那就再学习会 ActiveMQ 吧!现在官网的文档没啥好看的了,毕竟是入门学习,太深奥的东西也理解不了.然后看官网上有推荐书籍& ...

  9. Nginx实用教程(二):配置文件入门

    Nginx配置文件结构 nginx配置文件由指令(directive)组成,指令分为两种形式,简单指令和区块指令. 一条简单指令由指令名.参数和结尾的分号(;)组成,例如: listen backlo ...

  10. python http长连接客户端

    背景: 线上机器,需要过滤access日志,发送给另外一个api 期初是单进程,效率太低,改为多进程发送后,查看日志中偶尔会出现异常错误(忘记截图了...) 总之就是端口不够用了报错 原因: 每一条日 ...