示例JSON:
"#{self.end_date} 23:59:59".to_time.to_formatted_s(:db)
我使用杰克逊解析了同样的问题,但是想用GSON做这件事。
答案 0 :(得分:0)
这是一个最奇怪的回应我已经编写了便利模型类来重现响应我希望这有帮助
OutOfNamesMate
import com.google.gson.annotations.SerializedName;
public class OutOfNamesMate {
@SerializedName("hostname")
private String hostname;
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
}
端口
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Port {
@SerializedName("1521/tcp/oracle_tnslsnr")
private ArrayList<OutOfNamesMate> outOfNamesMates;
public ArrayList<OutOfNamesMate> getOutOfNamesMates() {
return outOfNamesMates;
}
public void setOutOfNamesMates(ArrayList<OutOfNamesMate> outOfNamesMates) {
this.outOfNamesMates = outOfNamesMates;
}
}
Output.java
import com.google.gson.annotations.SerializedName;
public class Output {
@SerializedName("ports")
private Port ports;
@SerializedName("has_attachment")
private String has_attachment;
@SerializedName("custom_description")
private String custom_description;
@SerializedName("plugin_output")
private String plugin_output;
@SerializedName("hosts")
private String hosts;
@SerializedName("severity")
private String severity;
public Port getPorts() {
return ports;
}
public void setPorts(Port ports) {
this.ports = ports;
}
public String getHas_attachment() {
return has_attachment;
}
public void setHas_attachment(String has_attachment) {
this.has_attachment = has_attachment;
}
public String getCustom_description() {
return custom_description;
}
public void setCustom_description(String custom_description) {
this.custom_description = custom_description;
}
public String getPlugin_output() {
return plugin_output;
}
public void setPlugin_output(String plugin_output) {
this.plugin_output = plugin_output;
}
public String getHosts() {
return hosts;
}
public void setHosts(String hosts) {
this.hosts = hosts;
}
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
}
响应
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("outputs")
private ArrayList<Output> output;
public ArrayList<Output> getOutput() {
return output;
}
public void setOutput(ArrayList<Output> output) {
this.output = output;
}
}
测试代码 以下是检查类是否正常工作的测试代码,
您可以在代码中使用上述模型类,但严格遵循代码来匹配您的数据,绝不是生成JSON
数据的标准方法,您需要如何填充你的对象
import java.util.ArrayList;
import com.google.gson.Gson;
public class TestDrive {
public static void main(String[] args) {
OutOfNamesMate mate = new OutOfNamesMate();
mate.setHostname("172.27.64.253");
ArrayList<OutOfNamesMate> mates = new ArrayList<>();
mates.add(mate);
Port port = new Port();
port.setOutOfNamesMates(mates);
Output output = new Output();
output.setPorts(port);
output.setHas_attachment("0");
output.setCustom_description("");
output.setPlugin_output("Nothing here");
output.setHosts("");
output.setSeverity("3");
String result = (new Gson()).toJson(output);
System.out.println(""+result);
}
}
输出
{
"ports": {
"1521/tcp/oracle_tnslsnr": [
{
"hostname": "172.27.64.253"
}
]
},
"has_attachment": "0",
"custom_description": "",
"plugin_output": "Nothing here",
"hosts": "",
"severity": "3"
}