我试图将Cucumber的Json输出转换为单个Java对象.这包含嵌套四层深度的对象,我无法反序列化它.我现在正在使用杰克逊,但愿意接受建议.这是我的Json代码:
{
"line": 1,
"elements": [
{
"line": 3,
"name": "Converteren centimeters naar voeten/inches",
"description": "",
"id": "applicatie-neemt-maten-in-cm-en-converteert-ze-naar-voet/inch,-en-vice-versa;converteren-centimeters-naar-voeten/inches",
"type": "scenario",
"keyword": "Scenario",
"steps": [
{
"result": {
"duration": 476796588,
"status": "passed"
},
"line": 4,
"name": "maak Maten-object aan met invoer in \"centimeters\"",
"match": {
"arguments": [
{
"val": "centimeters",
"offset": 37
}
],
"location": "StepDefinition.maakMatenObjectAanMetInvoerIn(String)"
},
"keyword": "Given "
},
{
"result": {
"duration": 36319,
"status": "passed"
},
"line": 5,
"name": "ik converteer",
"match": {
"location": "StepDefinition.converteerMaten()"
},
"keyword": "When …Run Code Online (Sandbox Code Playgroud) 这让我发疯。我想读取许多使用 Windows-1252 编码的文本文件(位于我的项目目录之外),将它们编辑为字符串,然后写回这些文件,再次作为 Windows-1252。然而 Visual Studio 却不断生成 UTF-8 文件。
这是我的文件读取代码:
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{
String s = sr.ReadToEnd();
return s;
}
Run Code Online (Sandbox Code Playgroud)
这是我的文件写入代码:
File.WriteAllText(fileName, joinedFileString, Encoding.Default);
Run Code Online (Sandbox Code Playgroud)
在这两者之间,我执行了各种编辑,包括添加、删除和 grep 换行符,但我认为这些可以通过在 File.WriteAllText 中指定编码来以正确的编码进行解析。请注意,我已在 Visual Studio 的“高级保存选项”中将默认编码更改为 1252。因此 Encoding.Default 应引用正确的编码。
但它不断将文件转换为 UTF-8!:-(
什么被认为是更好的Java代码?使对象变量并重复使用每个函数,或者为每个函数创建相同类型的新变量?例如:
public class FooDAO {
private PreparedStatement ps;
getApples(){
ps = connection.prepareStatement("GET * FROM apples");
...
}
getBananas(){
ps = connection.prepareStatement("GET * FROM bananas");
...
}
}
Run Code Online (Sandbox Code Playgroud)
相比:
public class FooDAO {
getApples(){
PreparedStatement ps = connection.prepareStatement("GET * FROM apples");
...
}
getBananas(){
PreparedStatement ps = connection.prepareStatement("GET * FROM bananas");
...
}
}
Run Code Online (Sandbox Code Playgroud)
哪种做法被认为是"清洁"?