java解析json

(1) Java解析JSON

建议使用 fastjson

(2) 使用 fastjson 时遇到的问题

(2.1) java.lang.VerifyError

1
2
java.lang.VerifyError: (class: com/alibaba/fastjson/parser/deserializer/FastjsonASMDeserializer_3_ErpOrderResVo, method: deserialze signature: (Lcom/alibaba/fastjson/parser/DefaultJSONParser;Ljava/lang/reflect/Type;Ljava/lang/Object;I)Ljava/lang/Object;) Accessing value from uninitialized register 85
at java.lang.Class.getDeclaredConstructors0(Native Method)

(2.2)fastjson 32个字段时反序列化报错的问题

1.2.28 32整数倍个字段 OOM bug
32个字段时反序列化报错

(3) 老办法

以下是老方法/笨办法

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxxx.file.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
* @version V1.0
* @date 2017-09-21 21:48
*/
public class JsonTest {

private static final Logger logger = LoggerFactory.getLogger(JsonTest.class);

public static void main(String[] args) {

String str = null;
String filePath = "doc/html/xiecheng/crawler_flights_bj-yc-2017-09-23_result.json";
try {
str = FileUtil.readFile2String(filePath);
} catch (IOException e) {
logger.error("", e);
}

JSONObject json = JSONObject.parseObject(str);
JSONArray fisArray = json.getJSONArray("fis");

int size = fisArray.size();
for(int i = 0; i < size; i++){
JSONObject oneFlight = (JSONObject) fisArray.get(i);
logger.info(i +": {}", oneFlight);
}

}


/**
*
* @param str
*/
public List<Map<String, String>> parserJson(String str) {

if (StringUtil.isEmpty(str)) {
logger.error("json为空");
return null;
}

JSONObject json = JSONObject.parseObject(str);

// 航空公司
JSONObject als = (JSONObject) json.get("als");
logger.info("符合要求的航空公司:{}", als);
Set<Map.Entry<String, Object>> set = als.entrySet();
logger.info("set:{}", set);

JSONObject apb = (JSONObject) json.get("apb");
logger.info("符合要求的机场:{}", apb);

// 中转组合,暂时不考虑,业务上也暂时不需要,稍微有点复杂,时间允许可以解析
// json.get("tf");

// 直航
JSONArray fisArray = json.getJSONArray("fis");
if (fisArray == null || fisArray.isEmpty()) {
logger.error("fisArray结果为空");
return null;
}

int size = fisArray.size();
if (size == 0) {
logger.error("fisArray 里元素个数为0");
return null;
}

List<Map<String, String>> list = new ArrayList<>(size);

for (int i = 0; i < size; i++) {

Map<String, String> dataMap = new HashMap<>(16);

//
JSONObject oneFlight = (JSONObject) fisArray.get(i);
logger.debug(i + ": {}", oneFlight);

// 到达城市代码
String acc = (String) oneFlight.get("acc");
dataMap.put("acc", acc);

// 目的地
String acn = (String) oneFlight.get("acn");
dataMap.put("acn", acn);

// 航空公司
String alc = (String) oneFlight.get("alc");
String flightCompany = null;
if (als.containsKey(alc)) {
flightCompany = als.getString(alc);
}
dataMap.put("alc", flightCompany);

// 到达机场
String apbn = (String) oneFlight.get("apbn");
dataMap.put("apbn", apbn);

// 到达时间
String at = (String) oneFlight.get("at");
dataMap.put("at", at);

// 到达城市代码
String dcc = (String) oneFlight.get("dcc");
dataMap.put("dcc", dcc);

// 出发地
String dcn = (String) oneFlight.get("dcn");
dataMap.put("dcn", dcn);

// 出发机场
String dpbn = (String) oneFlight.get("dpbn");
dataMap.put("dpbn", dpbn);

// 航站楼
String dsmsn = (String) oneFlight.get("dsmsn");
dataMap.put("dsmsn", dsmsn);

// 出发时间
String dt = (String) oneFlight.get("dt");
dataMap.put("dt", dt);

// 航班号
String fn = (String) oneFlight.get("fn");
dataMap.put("fn", fn);

// 实际票价
String lp = oneFlight.get("lp").toString();
dataMap.put("lp", lp);

JSONObject confort = oneFlight.getJSONObject("confort");
String historyPunctualityArr = (String) confort.get("HistoryPunctualityArr");
if (StringUtil.isNotEmpty(historyPunctualityArr)) {
dataMap.put("historyPunctualityArr", historyPunctualityArr.replace("%", ""));
}

logger.info("本条航班数据:{}", dataMap);

list.add(dataMap);
}

return list;

} // end method


}

References

[1] Java几种常用JSON库性能比较