ReadLocalJsonFileDemo.java
6.28 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.glsk.compute.brand;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author fangfang
* @create 2023-07-28 14:09
* 获取门店 区域(省份) 品牌得映射关系
*/
public class ReadLocalJsonFileDemo {
public static void main(String[] args) throws IOException {
// File file = new File("D:\\test.json");
File file = new File("src/main/resources/config/品牌.json");
JSONObject jsonObject = readerMethod2(file);
// 读取json文件的data字段,并转换为json数组
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("======================");
JSONArray array = jsonObject.getJSONArray("data");
List<Brand> brandList=new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JSONObject json = array.getJSONObject(i);
System.out.println("id="+json.getString("id"));
JSONArray children = JSON.parseArray(json.getString("children"));
if(JSON.parseArray(json.getString("children"))!=null){
for (int j = 0; j < children.size(); j++) {
Brand brand =new Brand();
brand.setId( json.getString("id"));
brand.setLabel(json.getString("label"));
brand.setLogoUrl(json.getString("logoUrl"));
JSONObject childrenjson = children.getJSONObject(j);
brand.setChildrenid(childrenjson.getString("id"));
brand.setChildrenlabel(childrenjson.getString("label"));
brand.setChildrenlogoUrl(childrenjson.getString("logoUrl"));
brand.setChildrenchildren(childrenjson.getString("children"));
brandList.add(brand);
}
}
}
// System.out.println(brandList.toString());
String fileName="src/main/resources/resoult/brandList.xlsx";
EasyExcel.write(fileName,Brand.class).sheet("Sheet1").doWrite(brandList);
}
public static JSONObject readerMethod(File file) throws IOException {
FileReader fileReader = new FileReader(file);
Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
int ch= 0;
StringBuffer sb = new StringBuffer();
while((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
String jsonStr = sb.toString();
return JSONObject.parseObject(jsonStr);
}
public static JSONObject readerMethod2(File file) throws IOException {
String filePath = "path/to/your/file.json";
StringBuilder contentBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
contentBuilder.append(line).append("\n");// 获取StringBuilder对象的字符串表示形式
}
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObj = JSONObject.parseObject(contentBuilder.toString()) ; // 将字符串解析为JSONObject对象
// 打印结果
System.out.println(jsonObj);
return jsonObj;
}
}
@Data
class Brand{
@ColumnWidth(10)
@ExcelProperty({"品牌id"})
private String id ;
@ColumnWidth(10)
@ExcelProperty({"品牌名"})
private String label ;
@ColumnWidth(10)
@ExcelProperty({"logo地址"})
private String logoUrl;
@ColumnWidth(10)
@ExcelProperty({"子品牌id"})
private String childrenid ;
@ColumnWidth(10)
@ExcelProperty({"子品牌名称"})
private String childrenlabel ;
@ColumnWidth(10)
@ExcelProperty({"子品牌logo"})
private String childrenlogoUrl;
@ColumnWidth(10)
@ExcelProperty({"子品牌"})
private String childrenchildren;
public String getChildrenid() {
return childrenid;
}
public void setChildrenid(String childrenid) {
this.childrenid = childrenid;
}
public String getChildrenlabel() {
return childrenlabel;
}
public void setChildrenlabel(String childrenlabel) {
this.childrenlabel = childrenlabel;
}
public String getChildrenlogoUrl() {
return childrenlogoUrl;
}
public void setChildrenlogoUrl(String childrenlogoUrl) {
this.childrenlogoUrl = childrenlogoUrl;
}
public String getChildrenchildren() {
return childrenchildren;
}
public void setChildrenchildren(String childrenchildren) {
this.childrenchildren = childrenchildren;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
}
class ChildrenBrand {
@ColumnWidth(10)
@ExcelProperty({"子品牌id"})
private Integer id ;
@ColumnWidth(10)
@ExcelProperty({"子品牌名称"})
private String label ;
@ColumnWidth(10)
@ExcelProperty({"子品牌logo"})
private String logoUrl;
@ColumnWidth(10)
@ExcelProperty({"子品牌"})
private String children;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
public String getChildren() {
return children;
}
public void setChildren(String children) {
this.children = children;
}
}