search
HomeJavajavaTutorialConversion between objects and json in java

Conversion between objects and json in java

Jul 18, 2017 pm 03:18 PM
javajavascriptjson

Sometimes object data will be converted into json data for project needs. The following is a personal implementation method based on project needs.

Convert json string to java object

Similarly convert the json string to json object first, and then convert the json object to java Object as shown below.

JSONObject obj = new JSONObject().fromObject(jsonStr);//Convert json string to json object

Convert json object to java Object

Person jb = (Person)JSONObject.toBean(obj,Person.class);//Convert the json object to a Person object

Convert java object to json string

Convert java object to json object first, then convert json object to json string

JSONObject json = JSONObject.fromObject(obj);//Convert java object to json object

String str = json.toString();//Convert json object to string

The data format needs to be used in the project:

[{"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}]

The content of the list object in the project

 1 JsonModel{node='xxx_2', preNode='', index='index_1', status='表达式1'} 2 JsonModel{node='xxx_14', preNode='', index='index_4', status='表达式5'} 3 JsonModel{node='xxx_3', preNode='', index='index_2', status='表达式2'} 4 JsonModel{node='xxx_4', preNode='', index='index_3', status='表达式3'} 5 JsonModel{node='xxx_5', preNode='xxx_2', index='index_4', status='表达式4'} 6 JsonModel{node='xxx_5', preNode='xxx_3', index='index_4', status='表达式5'} 7 JsonModel{node='xxx_5', preNode='xxx_14', index='index_4', status='表达式5'} 8 JsonModel{node='xxx_6', preNode='xxx_5', index='index_5', status='表达式6'} 9 JsonModel{node='xxx_7', preNode='xxx_6', index='index_6', status='表达式7'}10 JsonModel{node='xxx_8', preNode='xxx_7', index='index_4', status='表达式8'}11 JsonModel{node='xxx_9', preNode='xxx_8', index='index_5', status='表达式5'}12 JsonModel{node='xxx_10', preNode='xxx_8', index='index_7', status='表达式6'}13 JsonModel{node='xxx_11', preNode='xxx_10', index='index_8', status='表达式7'}14 JsonModel{node='xxx_11', preNode='xxx_9', index='index_8', status='表达式8'}15 JsonModel{node='xxx_12', preNode='xxx_11', index='index_8', status='表达式7'}16 JsonModel{node='xxx_12', preNode='xxx_4', index='index_8', status='表达式8'}17 JsonModel{node='xxx_13', preNode='xxx_14', index='', status='表达式13'}18 JsonModel{node='xxx_13', preNode='xxx_12', index='', status='表达式14'}

  testJson() <jsonmodel> list= ArrayList== JsonModel("xxx_2","","index_1","表达式1"= JsonModel("xxx_14","","index_4","表达式5"= JsonModel("xxx_3","","index_2","表达式2"= JsonModel("xxx_4","","index_3","表达式3"= JsonModel("xxx_5","xxx_2","index_4","表达式4"= JsonModel("xxx_5","xxx_3","index_4","表达式5"= JsonModel("xxx_5","xxx_14","index_4","表达式5"= JsonModel("xxx_6","xxx_5","index_5","表达式6"= JsonModel("xxx_7","xxx_6","index_6","表达式7"= JsonModel("xxx_8","xxx_7","index_4","表达式8"= JsonModel("xxx_9","xxx_8","index_5","表达式5"= JsonModel("xxx_10","xxx_8","index_7","表达式6"= JsonModel("xxx_11","xxx_10","index_8","表达式7"= JsonModel("xxx_11","xxx_9","index_8","表达式8"= JsonModel("xxx_12","xxx_11","index_8","表达式7"= JsonModel("xxx_12","xxx_4","index_8","表达式8"= JsonModel("xxx_13","xxx_14","","表达式13"= JsonModel("xxx_13","xxx_12","","表达式14"->String str=JsonNode rootNode =<childnode> preNode_set=<childnode> postNode_set=<node> nodeList= ArrayListNode node="0"<childnode> childNodeList= HashSet( i=0;i<rootnode.size>= HashSetJsonNode nameNode = (rootNode.get(i)).get("node"==rootNode.get(i).get("index"=rootNode.get(i).get("status"( j=i+1;j<rootnode.size>++++ ChildNode("0"++=Node pre_node="9999"<childnode> childNodes= HashSet( i=0;i<rootnode.size> nodeList= ArrayList==<jsonmodel> jsonModels= ArrayList( i=1;i<jsonnode.size></jsonnode.size></jsonmodel></rootnode.size></childnode></rootnode.size></rootnode.size></childnode></node></childnode></childnode></jsonmodel>

The following are the object entity classes used

 1 package com.yf.af.biz.test; 2  3 /** 4  * Created by chen on 2017/7/14. 5  */ 6 public class JsonModel { 7     private String node; 8     private String preNode; 9     private String index;10     private String status;11 12     public String getNode() {13         return node;14     }15 16     public void setNode(String node) {17         this.node = node;18     }19 20     public String getPreNode() {21         return preNode;22     }23 24     public void setPreNode(String preNode) {25         this.preNode = preNode;26     }27 28     public String getIndex() {29         return index;30     }31 32     public void setIndex(String index) {33         this.index = index;34     }35 36     public String getStatus() {37         return status;38     }39 40     public void setStatus(String status) {41         this.status = status;42     }43 44     public JsonModel(String node, String preNode, String index, String status) {45         this.node = node;46         this.preNode = preNode;47         this.index = index;48         this.status = status;49     }50 51     public JsonModel() {52     }53 54     @Override55     public String toString() {56         return "JsonModel{" +57                 "node='" + node + '\'' +58                 ", preNode='" + preNode + '\'' +59                 ", index='" + index + '\'' +60                 ", status='" + status + '\'' +61                 '}';62     }63 }
View Code
 1 package com.yf.af.biz.test; 2  3 import java.util.List; 4 import java.util.Set; 5  6 /** 7  * Created by chen on 2017/7/15. 8  */ 9 public class Node {10     private String node;11     private String index;12     private String status;13 14     private Set<childnode> preNode;15 16     private Set<childnode> postNode;17 18     public String getNode() {19         return node;20     }21 22     public void setNode(String node) {23         this.node = node;24     }25 26     public String getIndex() {27         return index;28     }29 30     public void setIndex(String index) {31         this.index = index;32     }33 34     public String getStatus() {35         return status;36     }37 38     public void setStatus(String status) {39         this.status = status;40     }41 42     public Set<childnode> getPreNode() {43         return preNode;44     }45 46     public void setPreNode(Set<childnode> preNode) {47         this.preNode = preNode;48     }49 50     public Set<childnode> getPostNode() {51         return postNode;52     }53 54     public void setPostNode(Set<childnode> postNode) {55         this.postNode = postNode;56     }57 58     @Override59     public String toString() {60         return "Node{" +61                 "node='" + node + '\'' +62                 ", index='" + index + '\'' +63                 ", status='" + status + '\'' +64                 ", preNode=" + preNode +65                 ", postNode=" + postNode +66                 '}';67     }68 69 70     public Node() {71     }72 }</childnode></childnode></childnode></childnode></childnode></childnode>
View Code
 1 package com.yf.af.biz.test; 2  3 /** 4  * Created by chen on 2017/7/15. 5  */ 6 public class ChildNode { 7     private String node; 8  9     public String getNode() {10         return node;11     }12 13     public void setNode(String node) {14         this.node = node;15     }16 17     public ChildNode(String node) {18         this.node = node;19     }20 21     public ChildNode() {22     }23 }
View Code

The above is the detailed content of Conversion between objects and json in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools