繁体   English   中英

我如何使用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 时将出现无限循环或堆栈溢出

为什么不直接用需要的字段创建自己的类,然后使用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
}

示例类是:

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;
    }
}

注意:示例类必须具有所有字段的 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"
        }]
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM