<div class="container valign-transform">
<div class="row">
<div class="col-sm-12">
<h4>Text</h4>
</div>
</div>
</div>
.vertical-center {
position: relative;
top: 50%;
z-index: 2;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
}
嘿,如果用户输入版本号,则从代码中创建一个JSON,它不应该在root jsonobject中添加。所以如何限制它。
Scanner input = new Scanner(System.in);
JSONArray finaljson = new JSONArray();
for (int j = 0; j < 2; j++) {
System.out.println("Enter Version Name");
String vName = input.next();
System.out.println("Enter Version Key");
;
String vKey = input.next();
JSONObject root = new JSONObject();
if (!root.has("versionName")) {
root.put("versionName", vName);
root.put("versionKey", vKey);
}
JSONArray issue = new JSONArray();
System.out.println("Enter Epic Name");
String epicName = input.next();
System.out.println("Enter Epic Key");
String epicKey = input.next();
JSONObject epicData = new JSONObject();
epicData.put("epickKey", epicKey);
epicData.put("epickName", epicName);
issue.put(epicData);
root.put("issue", issue);
finaljson.put(root);
}
System.out.println("JSON DATA" + finaljson.toString());
但想要
[
{
"versionKey": "vkey1",
"issue": [
{
"epickName": "e1",
"epickKey": "ekey1"
}
],
"versionName": "v1"
},
{
"versionKey": "vkey1",
"issue": [
{
"epickName": "e2",
"epickKey": "eky2"
}
],
"versionName": "v1"
}
]
你能帮助我如何制作这种类型的动态json数据
答案 0 :(得分:1)
如果我理解正确,您希望输出类似于您问题中的第二个JSON数组。改变循环的位置应该可以解决问题:
Scanner input = new Scanner(System.in);
JSONArray finaljson = new JSONArray();
System.out.println("Enter Version Name");
String vName = input.next();
System.out.println("Enter Version Key");;
String vKey = input.next();
JSONObject root = new JSONObject();
if (!root.has("versionName")) {
root.put("versionName", vName);
root.put("versionKey", vKey);
}
JSONArray issue = new JSONArray();
for (int j = 0; j < 2; j++) {
System.out.println("Enter Epic Name");
String epicName = input.next();
System.out.println("Enter Epic Key");
String epicKey = input.next();
JSONObject epicData = new JSONObject();
epicData.put("epickKey", epicKey);
epicData.put("epickName", epicName);
issue.put(epicData);
}
root.put("issue", issue);
finaljson.put(root);
System.out.println("JSON DATA" + finaljson.toString());
我测试了你的输入。这是格式化的JSON输出:
[
{
"issue": [
{
"epickKey": "eky1",
"epickName": "e1"
},
{
"epickKey": "eky2",
"epickName": "e2"
}
],
"versionName": "vkey1",
"versionKey": "v1"
}
]
答案 1 :(得分:0)
我更改了你的代码并添加了另外两个类。一个名为Data for(versionKey,nameKey),第二个名为Epic for(epickName,epickKey) 并生成Json,我使用Gson lib。 这段代码:
史诗类:
public class Epic {
private String epicName;
private String epicKey;
public Epic() {
}
public Epic(String epicName, String epicKey) {
this.epicName = epicName;
this.epicKey = epicKey;
}
public String getEpicName() {
return epicName;
}
public void setEpicName(String epicName) {
this.epicName = epicName;
}
public String getEpicKey() {
return epicKey;
}
public void setEpicKey(String epicKey) {
this.epicKey = epicKey;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Epic [epicName=");
builder.append(epicName);
builder.append(", epicKey=");
builder.append(epicKey);
builder.append("]");
return builder.toString();
}
}
数据类:
import java.util.List;
public class Data {
private String versionName;
private String versionKey;
private List<Epic> epics;
public Data() {
}
public Data(String versionName, String versionKey, List<Epic> epics) {
this.versionName = versionName;
this.versionKey = versionKey;
this.epics = epics;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getVersionKey() {
return versionKey;
}
public void setVersionKey(String versionKey) {
this.versionKey = versionKey;
}
public List<Epic> getEpics() {
return epics;
}
public void setEpics(List<Epic> epics) {
this.epics = epics;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((versionKey == null) ? 0 : versionKey.hashCode());
result = prime * result
+ ((versionName == null) ? 0 : versionName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Data other = (Data) obj;
if (versionKey == null) {
if (other.versionKey != null)
return false;
} else if (!versionKey.equals(other.versionKey))
return false;
if (versionName == null) {
if (other.versionName != null)
return false;
} else if (!versionName.equals(other.versionName))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Data [versionName=");
builder.append(versionName);
builder.append(", versionKey=");
builder.append(versionKey);
builder.append(", epics=");
builder.append(epics);
builder.append("]");
return builder.toString();
}
}
主类:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<Data> datas = new ArrayList<Data>();
boolean add = false;
for (int j = 0; j < 2; j++) {
add = false;
Data data = new Data();
System.out.println("Enter Version Name");
data.setVersionName(input.next());
System.out.println("Enter Version Key");
data.setVersionKey(input.next());
Epic epic = new Epic();
System.out.println("Enter Epic Name");
epic.setEpicName(input.next());
System.out.println("Enter Epic Key");
epic.setEpicKey(input.next());
List<Epic> epics = new ArrayList<Epic>();
if(datas.isEmpty()){
epics.add(epic);
data.setEpics(epics);
datas.add(data);
}else{
for(Data d:datas){
if(d.equals(data)){
d.getEpics().add(epic);
add=true;
}
}
if(!add){
epics.add(epic);
data.setEpics(epics);
datas.add(data);
}
}
}
//Gson gson = new GsonBuilder().setPrettyPrinting().create();
//System.out.println(gson.toJson(datas));
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(datas);
System.out.println(jsonArray.toString());
}
}
PS:要下载Gson API
的网址修改强>
我在Main Class中进行了更改以使用json-lib-2.4-jdk15.jar 看看这个网址: json-lib
答案 2 :(得分:0)
使用Gson读取你的json:
为您的Json对象定义Pojos:
public class Epic {
String versionKey, versionName;
Issue issue;
}
public class Issue{
String epicName, epicKey;
}
使用Gson从json加载Pojos
// Create gson object
Gson gson = new GsonBuilder().create();
// Load your json objects:
BufferedReader br = new BufferedReader(new FileReader("file.json"));
List<Epic> epics = gson.fromJson(br, new TypeToken<ArrayList<Epic>>() {}.getType());
为“多个问题 - 史诗”创建所需的类结构:
// This will be used to store in the format you want
public class MultiIssueEpic{
public String versionKey, versionName;
public Set<Issue> issueSet;
public MultiIssueEpic(Epic epic){
this.versionName = epic.versionName;
this.versionKey = epic.versionKey;
this.issueSet = new HashSet<>();
this.issueSet.add(epic.issue);
}
}
从MultiIssueEpic
:
List<Epic>
Map<String, MultiIssueEpic> epicMap = new HashMap<>();
for(Epic epic : epics){
if(epicMap.contains(epic.versionName)){
// Add issue to existing MultiIssueEpic
epicMap.get(epic.versionName).issueSet.add(epic.issue);
} else{
// Add new MultiIssueEpic
epicMap.put(epic.versionName, new MultiIssueEpic(epic));
}
}
// Here is your list of MultiIssueEpics:
List<MultiIssueEpic> multiIssueEpics = new ArrayList<>(epicMap.values());