java:Json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | /** * encoding: utf-8 * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述: * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2023 - 2023/11/16 - 12:29 * # User : geovindu * # Product : IntelliJ IDEA * # Project : javademo * # File : User.java 类 * # explain : 学习 **/ package Model; import java.util.Objects; /** * 用户 * @author geovindu * */ public class User { /** *姓名 */ private String name; /** *职业 */ private String occupation; /** *兄弟姐妹有几人 */ private int siblings; /** *身高 */ private double height; /** *结婚否 */ private boolean married; /** * * @param name * @param occupation * @param siblings * @param height * @param married */ public User(String name, String occupation, int siblings, double height, boolean married) { this .name = name; this .occupation = occupation; this .siblings = siblings; this .height = height; this .married = married; } /** *获取姓名 * @return */ public String getName() { return name; } /** *设置姓名 * @param name */ public void setName(String name) { this .name = name; } /** * * @return */ public String getOccupation() { return occupation; } /** * * @param occupation */ public void setOccupation(String occupation) { this .occupation = occupation; } /** * * @return */ public int getSiblings() { return siblings; } /** * * @param siblings */ public void setSiblings( int siblings) { this .siblings = siblings; } /** * * @return */ public double getHeight() { return height; } /** * * @param height */ public void setHeight( double height) { this .height = height; } /** * * @return */ public boolean isMarried() { return married; } /** * * @param married */ public void setMarried( boolean married) { this .married = married; } /** * * @param o * @return */ @Override public boolean equals(Object o) { if ( this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; User user = (User) o; return siblings == user.siblings && Double.compare(user.height, height) == 0 && married == user.married && Objects.equals(name, user.name) && Objects.equals(occupation, user.occupation); } /** * * @return */ @Override public int hashCode() { return Objects.hash(name, occupation, siblings, height, married); } /** * * @return */ @Override public String toString() { final StringBuilder sb = new StringBuilder( "User{" ); sb.append( "name='" ).append(name).append('\ '' ); sb.append( ", occupation='" ).append(occupation).append('\ '' ); sb.append( ", siblings=" ).append(siblings); sb.append( ", height=" ).append(height); sb.append( ", married=" ).append(married); sb.append( '}' ); return sb.toString(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | /** * encoding: utf-8 * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述:https://www.tutorialspoint.com/org_json/org_json_cdl.htm * https://www.baeldung.com/java-converting-json-to-csv * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2023 - 2023/11/16 - 21:24 * # User : geovindu * # Product : IntelliJ IDEA * # Project : javademo * # File : UserBll.java 类 * # explain : 学习 **/ package BLL; import org.json.JSONObject; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONWriter; import org.json.CDL; import org.json.JSONTokener; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.logging.*; import java.util.concurrent.locks.*; import Model.User; import java.io.*; import java.util.stream.Stream; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.json.*; import com.fasterxml.jackson.dataformat.csv.*; import com.fasterxml.jackson.core.util.*; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.MappingIterator; import com.fasterxml.jackson.databind.introspect.*; import Model.OrderLineForCsv; import Model.OrderLine; public class UserBll { /** * */ public static void getUser() { var user = new Model.User( "John Doe" , "gardener" , 2 , 172.35 , true ); var userjo = new JSONObject(user); System.out.println(userjo); System.out.println(userjo.get( "name" )); System.out.println(userjo.get( "occupation" )); System.out.println(userjo.get( "siblings" )); } /** * */ public static void getUserArry() { var user = new JSONObject(); user.put( "name" , "John Doe" ); user.put( "occupation" , "gardener" ); user.put( "siblings" , Integer.valueOf( 2 )); user.put( "height" , Double.valueOf( 172.35 )); user.put( "married" , Boolean.TRUE); var cols = new JSONArray(); cols.put( "red" ); cols.put( "blue" ); cols.put( "navy" ); user.put( "favCols" , cols); var userJson = user.toString(); System.out.println(userJson); } /** * */ public static void getWrite() { var user = new StringBuilder(); var writer = new JSONWriter(user); writer.object(); writer.key( "name" ).value( "John Doe" ); writer.key( "occupation" ).value( "gardener" ); writer.key( "siblings" ).value( 2 ); writer.key( "married" ).value( true ); writer.key( "favCols" ); writer.array(); writer.value( "red" ); writer.value( "blue" ); writer.value( "navy" ); writer.endArray(); writer.endObject(); System.out.println(user); } /** *name, occupation, siblings, height, married */ public static void getCsvtoJson() { System.setProperty( "java.util.logging.config.file" , "logging/logging.properties" ); Logger logger=Logger.getLogger( "com.darwinsys" ); String csvData= "empId, name, age \n" + "1, Mark, 22 \n" + "2, Robert, 35 \n" + "3, Julia, 18" ; System.out.println(CDL.toJSONArray(csvData)); //Case 4: CSV without header JSONArray jsonArray = new JSONArray(); jsonArray.put( "empId" ); jsonArray.put( "name" ); jsonArray.put( "age" ); csvData = "1, Mark, 22 \n" + "2, Robert, 35 \n" + "3, Julia, 18" ; System.out.println(CDL.toJSONArray(jsonArray,csvData)); try { /* CsvMapper csvMapper = new CsvMapper(); CsvSchema csvSchema = csvMapper .schemaFor(OrderLineForCsv.class) .withHeader(); OrderLine[] orderLines = new ObjectMapper() .readValue(new File("src/resources/orderLines.json"), OrderLine[].class); csvMapper.writerFor(OrderLine[].class) .with(csvSchema) .writeValue(new File("src/resources/data.csv"), orderLines);*/ /* CsvSchema orderLineSchema = CsvSchema.emptySchema().withHeader(); CsvMapper csvMapper = new CsvMapper(); MappingIterator<OrderLine> orderLines = csvMapper.readerFor(OrderLine.class) .with(orderLineSchema) .readValues(new File("src/resources/data.csv")); System.out.println(orderLines.readAll()); new ObjectMapper() .configure(SerializationFeature.INDENT_OUTPUT, true) .writeValue(new File("src/resources/orderLinesFromCsv.json"), orderLines.readAll()); */ var data = Files.readString(Paths.get( "src/resources/data.csv" ), StandardCharsets.UTF_8); System.out.println(data); jsonArray = new JSONArray(); jsonArray.put( "name" ); jsonArray.put( "occupation" ); jsonArray.put( "siblings" ); jsonArray.put( "height" ); jsonArray.put( "married" ); //读了标题,只读了一行 JSONArray usersJson = CDL.toJSONArray(jsonArray,data); if (usersJson== null ) { System.out.println( "字符串转JSON错误" ); } else { System.out.println(CDL.toJSONArray(jsonArray, new JSONTokener(data))); } //usersJson.forEach(obj->System.out.println(obj)); System.out.println( "user:" +usersJson.length()); usersJson.forEach(obj->{String name = ((JSONObject) obj).getString( "name" ); System.out.println(name); //String occupation = ((JSONObject) obj).getString("occupation"); }); /* Stream.iterate(0,integer -> integer+1) .limit(usersJson.length()).forEach(index->{ System.out.println("index==>"+index); System.out.println(usersJson.get(index)); });*/ } catch (Exception exception) { logger.log(Level.SEVERE, "Caught Exception" ,exception); //LogRecord msg=new LogRecord(Level.SEVERE,"Caught Exception"); //msg.setThrown(exception); //logger.log(msg); System.out.println(exception.getMessage()); } } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /** * encoding: utf-8 * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述: * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2023 - 2023/11/15 - 6:02 * # User : geovindu * # Product : IntelliJ IDEA * # Project : javademo * # File : main.java 类 * # explain : 学习 *GraalVM * https://www.graalvm.org/ * https://www.oracle.com/cn/java/graalvm/ * https://www.graalvm.org/downloads/# * com.darwinsys *CVS,Subversion,Git * http://git-scm.com */ import com.darwinsys.util.*; import Common.Env; import Common.JsonHelper; import BLL.UserBll; public class Main { /** * * @param args */ public static void main(String[] args) { System.out.println( "Hello java language world! 涂聚文!" ); UserBll bll= new UserBll(); UserBll.getUser(); UserBll.getUserArry(); UserBll.getWrite(); UserBll.getCsvtoJson(); System.out.println(Env.getEnv()); System.out.println(Env.getColor()); Env.getEnvList(); } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-11-16 CSharp: Facade Pattern in donet 6
2018-11-16 MySQL5.7: datetime
2017-11-16 javascript: checked 不可全选
2012-11-16 Csharp: Treeview check list value
2011-11-16 Csharp DataGridView自定义添加DateTimePicker控件日期列