我需要这种格式的JSON输出。
{ "DDA":{
"CustomerId":"String content",
"Login":{
"Password":"String content",
"Username":"String content"
},
"NodeID":"String content"
}
}
这是我的java课程。
package com.dvs.request;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.dvs.support.Login;
import com.dvs.support.DDA;
@XmlRootElement(name="DDA")
@XmlType(name = "Login", propOrder = {"DDA","Login"})
@XmlAccessorType(XmlAccessType.FIELD)
public class LoginRequest {
private DDA dda;
private Login login;
private String CustomerId;
private String nodeID;
@XmlElement
public DDA getDda() {
return dda;
}
public void setDda(DDA dda) {
this.dda = dda;
}
@XmlAttribute
public String getCustomerId() {
return CustomerId;
}
public void setCustomerId(String customerId) {
CustomerId = customerId;
}
@XmlAttribute
public String getNodeID() {
return nodeID;
}
public void setNodeID(String nodeID) {
this.nodeID = nodeID;
}
}
我应该如何处理用户名和密码字段以及如何以我想要的格式获取它们?我尝试将用户名和密码放在不同的类中,但我得到了#34;非法注释异常"。
答案 0 :(得分:0)
伪代码:
mainJsonObject = new JsonObject();
ddaObject = new JsonObject();
mainJsonObject.add("DDA", ddaObject);
ddaObject.add("CustomerId", "String content");
loginObject = new JsonObject();
ddaObject.add("Login", loginObject);
loginObject.add("Password", "String content");
loginObject.add("Username", "String content");
ddaObject.add("NodeID", "String content");
finalJsonString = mainJsonObject.toJsonString();
答案 1 :(得分:0)
这可能会有所帮助
JSONObject DDA = new JSONObject();
JSONObject obj = new JSONObject();
JSONObject obj1 = new JSONObject();
obj1.put("Password", "String Content");
obj1.put("Username", "String Content");
obj.put("CustomerId", "String Content");
obj.put("Login", obj1);
obj.put("NodeID", "String Content");
DDA.put("DDA", obj);