如何使用 javaobject 创建 json。?
how can i create a json using javaobject.?
如何使用 javaobject 创建 json。 ?
我做到了,但出现错误。
我需要构建一个正文以在 PUT 请求中进行解析。
如果有人能帮助我。
public static String generateJSON(String status, String assignee, String comment,String data, String filename, String contentType) throws IOException {
JSONArray jsonArray = new JSONArray();
JSONObject statusObj = new JSONObject();
statusObj.put("status", status);
statusObj.put("comment", comment);
statusObj.put("assignee", assignee);
statusObj.put("comment", comment);
JSONObject evidenceObj = new JSONObject();
evidenceObj.put("evidence", "newXML");
evidenceObj.put("data", data);
evidenceObj.put("filename", filename);
evidenceObj.put("contentType", contentType);
// Add the objects to the jsonArray
jsonArray.add(evidenceObj);
// Add the key tests jsonObject to the jsonArray
evidenceObj.put("add", jsonArray);
String jsonString = evidenceObj.toString();
return jsonString;
}
jsonArray.add(evidenceObj);
// ...
evidenceObj.put("add", jsonArray);
你不能那样做,存在循环依赖。
Java 不防止这种行为,当您导出 json
时,您将遇到无限循环或堆栈溢出
为什么不直接用需要的字段创建自己的 Class,然后使用 jackson
库将其转换为 JSON 字符串,使用方法 writeValueAsString()
。
示例:
import com.fasterxml.jackson.databind.ObjectMapper;
public static void main(String[] args) throws Exception
{
Example exObject = new Example();
exObject.setField1("data");
exObject.setField2("second data");
exObject.setField3(72);
System.out.println(new ObjectMapper().writeValueAsString(exObject));
}
日志控制台:
Output:
{
"field1" : "data",
"field2" : "second data",
"field3" : 72
}
示例 class 是:
private static class Example
{
private String field1;
private String field2;
private int field3;
public String getField1()
{
return field1;
}
public void setField1(String field1)
{
this.field1 = field1;
}
public String getField2()
{
return field2;
}
public void setField2(String field2)
{
this.field2 = field2;
}
public int getField3()
{
return field3;
}
public void setField3(int field3)
{
this.field3 = field3;
}
}
注意:例子class必须有getter + setter所有字段的方法
感谢@Adir D 的回复...我在我的代码中应用了它,但是我在实现数组和对象时遇到了一些问题
我的代码示例:
public static String exampleJSON(String fileName) throws JsonProcessingException {
//Convert the file to base64
File file = new File(Setup.xmlPath+fileName);
String data = encodeFileToBase64(file);
// create `ObjectMapper` instance
ObjectMapper mapper = new ObjectMapper();
//Create a object and set the information that will be sent to update the test
UpdateFieldsJSON fields = new UpdateFieldsJSON();
//Define map which will be converted to JSON
UploadEvidence[] evidence = Stream.of(
new UploadEvidence("data", fileName,"plain/text"))
.toArray(UploadEvidence[]::new);
fields.setStatus(fields.getStatus());
fields.setComment(fields.getComment());
fields.setAssignee("");
fields.setAdd(evidence);
fields.setEvidences("{"); // I need to create the object and insert the array
//Convert Object to JSON
String objecToJSon = mapper.writeValueAsString(fields);
return objecToJSon;
}
This is the output of my code...
{
"status": "PASS",
"comment": "...",
"assignee": "Miranda Bruno",
"evidences": null,
"add": [{
"data": "data",
"filename": "1.1newSpot.xml",
"contentType": "plain/text"
}]
}
我需要的是:
{
"status": "PASS",
"comment": "comment about the test",
"assignee": "admin",
"evidences": {
"add": [{
"data": "base64",
"filename": "example.txt",
"contentType": "plain/text"
}]
}
}
如何使用 javaobject 创建 json。 ? 我做到了,但出现错误。
我需要构建一个正文以在 PUT 请求中进行解析。 如果有人能帮助我。
public static String generateJSON(String status, String assignee, String comment,String data, String filename, String contentType) throws IOException {
JSONArray jsonArray = new JSONArray();
JSONObject statusObj = new JSONObject();
statusObj.put("status", status);
statusObj.put("comment", comment);
statusObj.put("assignee", assignee);
statusObj.put("comment", comment);
JSONObject evidenceObj = new JSONObject();
evidenceObj.put("evidence", "newXML");
evidenceObj.put("data", data);
evidenceObj.put("filename", filename);
evidenceObj.put("contentType", contentType);
// Add the objects to the jsonArray
jsonArray.add(evidenceObj);
// Add the key tests jsonObject to the jsonArray
evidenceObj.put("add", jsonArray);
String jsonString = evidenceObj.toString();
return jsonString;
}
jsonArray.add(evidenceObj);
// ...
evidenceObj.put("add", jsonArray);
你不能那样做,存在循环依赖。 Java 不防止这种行为,当您导出 json
时,您将遇到无限循环或堆栈溢出为什么不直接用需要的字段创建自己的 Class,然后使用 jackson
库将其转换为 JSON 字符串,使用方法 writeValueAsString()
。
示例:
import com.fasterxml.jackson.databind.ObjectMapper;
public static void main(String[] args) throws Exception
{
Example exObject = new Example();
exObject.setField1("data");
exObject.setField2("second data");
exObject.setField3(72);
System.out.println(new ObjectMapper().writeValueAsString(exObject));
}
日志控制台:
Output:
{
"field1" : "data",
"field2" : "second data",
"field3" : 72
}
示例 class 是:
private static class Example
{
private String field1;
private String field2;
private int field3;
public String getField1()
{
return field1;
}
public void setField1(String field1)
{
this.field1 = field1;
}
public String getField2()
{
return field2;
}
public void setField2(String field2)
{
this.field2 = field2;
}
public int getField3()
{
return field3;
}
public void setField3(int field3)
{
this.field3 = field3;
}
}
注意:例子class必须有getter + setter所有字段的方法
感谢@Adir D 的回复...我在我的代码中应用了它,但是我在实现数组和对象时遇到了一些问题 我的代码示例:
public static String exampleJSON(String fileName) throws JsonProcessingException {
//Convert the file to base64
File file = new File(Setup.xmlPath+fileName);
String data = encodeFileToBase64(file);
// create `ObjectMapper` instance
ObjectMapper mapper = new ObjectMapper();
//Create a object and set the information that will be sent to update the test
UpdateFieldsJSON fields = new UpdateFieldsJSON();
//Define map which will be converted to JSON
UploadEvidence[] evidence = Stream.of(
new UploadEvidence("data", fileName,"plain/text"))
.toArray(UploadEvidence[]::new);
fields.setStatus(fields.getStatus());
fields.setComment(fields.getComment());
fields.setAssignee("");
fields.setAdd(evidence);
fields.setEvidences("{"); // I need to create the object and insert the array
//Convert Object to JSON
String objecToJSon = mapper.writeValueAsString(fields);
return objecToJSon;
}
This is the output of my code...
{
"status": "PASS",
"comment": "...",
"assignee": "Miranda Bruno",
"evidences": null,
"add": [{
"data": "data",
"filename": "1.1newSpot.xml",
"contentType": "plain/text"
}]
}
我需要的是:
{
"status": "PASS",
"comment": "comment about the test",
"assignee": "admin",
"evidences": {
"add": [{
"data": "base64",
"filename": "example.txt",
"contentType": "plain/text"
}]
}
}