JSON 将数组与字符串数组组合以获得内聚的名称值对
JSON Combine Array with Array of Strings to get a cohesive name value pair
我的应用程序正在利用第 3 方应用程序获取数据 (Splunk)。 Splunks api 端点 returns 的输出是一个包含所有行 headers 的数组和一个包含所有行数据的字符串数组。例如
{
"fields":[
"appID",
"ApplicationName",
"AppOwner",
"AppOwnerID",
"KnownIPS",
"IP Count",
"KnownFIDS",
"FIDCount",
"LastSeen",
"TotalConnections"],
"rows":[
[
"123456",
"HelloWorld",
"Last,First",
"E12345",
"11.111.11.111,222.22.22.222",
"2",
"A67890,B12345,C67890",
"3",
"2019-12-08",
"47937"
]
],
"id":0
}
但是我希望我的输出类似于
{
Field[0]:row[0],
Field[1]:row[1],
etc..
}
现在我可以使用以下方法在我的网页上显示结果
try {
ArrayList<String> fieldslist = new ArrayList<String>();
JSONObject json = new JSONObject(responseString);
JSONArray fields = json.getJSONArray("fields");
JSONArray jsonArray = json.getJSONArray("rows"); // JSONArray is from the json.org library
String[][] arrayOfArrays = new String[jsonArray.length()][];
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray innerJsonArray = (JSONArray) jsonArray.get(i);
String[] stringArray = new String[innerJsonArray.length()];
for (int j = 0; j < innerJsonArray.length(); j++) {
stringArray[j] = (String) innerJsonArray.get(j);
}
arrayOfArrays[i] = stringArray;
}
if (fields != null) {
int len = fields.length();
for (int i=0;i<len;i++){
fieldslist.add(fields.get(i).toString());
}
} ;
appDetail.setFields(fieldslist);
appDetail.setRows(arrayOfArrays);
} catch (JSONException e) {
e.printStackTrace();
}
return appDetail;
还有我的模特
@JsonProperty("fields")
private List<String> fields = new ArrayList<String>();
@JsonProperty("rows")
private String[][] rows = new String[i][j];
@JsonProperty("fields")
public List<String> getFields() {
return fields;
}
@JsonProperty("fields")
public void setFields(List<String> fields) {
this.fields = fields;
}
public Model withFields(List<String> fields) {
this.fields = fields;
return this;
}
@JsonProperty("rows")
public String[][] getRows() {
return rows;
}
@JsonProperty("rows")
public void setRows(String[][] rows) {
this.rows = rows;
}
public Model withRows(String[][] rows) {
this.rows = rows;
return this;
我知道我必须更新我的模型才能正确显示正确的结果,但我似乎无法在 try catch 中获得正确的逻辑。
一个简单的方法是将fields
的对象的响应转换为List<String>
,rows
转换为List<List<String>>
,然后您可以将预期结果显示为如下。
Class SplunkResponse
class SplunkResponse {
private List<String> fields;
private List<List<String>> rows;
private int id;
//general getters ans setters
}
代码段
ObjectMapper mapper = new ObjectMapper();
SplunkResponse response = mapper.readValue(jsonStr, SplunkResponse.class);
Map<String, Object> resultMap = new HashMap<>();
for (int row = 0; row < response.getRows().size(); row++) {
for (int idx = 0; idx < response.getRows().get(row).size(); idx++) {
resultMap.put(response.getFields().get(idx), response.getRows().get(row).get(idx));
}
}
System.out.println(mapper.writeValueAsString(resultMap));
控制台输出
{"IP Count":"2","ApplicationName":"HelloWorld","AppOwner":"Last,First","KnownFIDS":"A67890,B12345,C67890","KnownIPS":"11.111.11.111,222.22.22.222","appID":"123456","AppOwnerID":"E12345","FIDCount":"3","TotalConnections":"47937","LastSeen":"2019-12-08"}
我的应用程序正在利用第 3 方应用程序获取数据 (Splunk)。 Splunks api 端点 returns 的输出是一个包含所有行 headers 的数组和一个包含所有行数据的字符串数组。例如
{
"fields":[
"appID",
"ApplicationName",
"AppOwner",
"AppOwnerID",
"KnownIPS",
"IP Count",
"KnownFIDS",
"FIDCount",
"LastSeen",
"TotalConnections"],
"rows":[
[
"123456",
"HelloWorld",
"Last,First",
"E12345",
"11.111.11.111,222.22.22.222",
"2",
"A67890,B12345,C67890",
"3",
"2019-12-08",
"47937"
]
],
"id":0
}
但是我希望我的输出类似于
{
Field[0]:row[0],
Field[1]:row[1],
etc..
}
现在我可以使用以下方法在我的网页上显示结果
try {
ArrayList<String> fieldslist = new ArrayList<String>();
JSONObject json = new JSONObject(responseString);
JSONArray fields = json.getJSONArray("fields");
JSONArray jsonArray = json.getJSONArray("rows"); // JSONArray is from the json.org library
String[][] arrayOfArrays = new String[jsonArray.length()][];
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray innerJsonArray = (JSONArray) jsonArray.get(i);
String[] stringArray = new String[innerJsonArray.length()];
for (int j = 0; j < innerJsonArray.length(); j++) {
stringArray[j] = (String) innerJsonArray.get(j);
}
arrayOfArrays[i] = stringArray;
}
if (fields != null) {
int len = fields.length();
for (int i=0;i<len;i++){
fieldslist.add(fields.get(i).toString());
}
} ;
appDetail.setFields(fieldslist);
appDetail.setRows(arrayOfArrays);
} catch (JSONException e) {
e.printStackTrace();
}
return appDetail;
还有我的模特
@JsonProperty("fields")
private List<String> fields = new ArrayList<String>();
@JsonProperty("rows")
private String[][] rows = new String[i][j];
@JsonProperty("fields")
public List<String> getFields() {
return fields;
}
@JsonProperty("fields")
public void setFields(List<String> fields) {
this.fields = fields;
}
public Model withFields(List<String> fields) {
this.fields = fields;
return this;
}
@JsonProperty("rows")
public String[][] getRows() {
return rows;
}
@JsonProperty("rows")
public void setRows(String[][] rows) {
this.rows = rows;
}
public Model withRows(String[][] rows) {
this.rows = rows;
return this;
我知道我必须更新我的模型才能正确显示正确的结果,但我似乎无法在 try catch 中获得正确的逻辑。
一个简单的方法是将fields
的对象的响应转换为List<String>
,rows
转换为List<List<String>>
,然后您可以将预期结果显示为如下。
Class SplunkResponse
class SplunkResponse {
private List<String> fields;
private List<List<String>> rows;
private int id;
//general getters ans setters
}
代码段
ObjectMapper mapper = new ObjectMapper();
SplunkResponse response = mapper.readValue(jsonStr, SplunkResponse.class);
Map<String, Object> resultMap = new HashMap<>();
for (int row = 0; row < response.getRows().size(); row++) {
for (int idx = 0; idx < response.getRows().get(row).size(); idx++) {
resultMap.put(response.getFields().get(idx), response.getRows().get(row).get(idx));
}
}
System.out.println(mapper.writeValueAsString(resultMap));
控制台输出
{"IP Count":"2","ApplicationName":"HelloWorld","AppOwner":"Last,First","KnownFIDS":"A67890,B12345,C67890","KnownIPS":"11.111.11.111,222.22.22.222","appID":"123456","AppOwnerID":"E12345","FIDCount":"3","TotalConnections":"47937","LastSeen":"2019-12-08"}