Bruno Lorena
Bruno Lorena

Reputation: 93

how can i create a json using javaobject.?

how can i create a json using javaobject. ? I did it, but I'm getting an error.

I need to build a body to parse within the PUT request. If anyone can help me.

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

Upvotes: 0

Views: 94

Answers (3)

Bruno Lorena
Bruno Lorena

Reputation: 93

Thanks for the reply @Adir D... I'm applying it here in my code, but I'm having some problems implementing the array and the object Example of my code:

   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"
    }]
}

What I need is:

{
    "status": "PASS",
    "comment": "comment about the test",
    "assignee": "admin",
    "evidences": {
        "add": [{
            "data": "base64",
            "filename": "example.txt",
            "contentType": "plain/text"
        }]
    }
}

Upvotes: 0

Adir Dayan
Adir Dayan

Reputation: 1627

Why not just to create your own Class with needed fields, and then use jackson library to convert it to JSON string, use method writeValueAsString().

Example:

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

Log console:

Output:
{
  "field1" : "data",
  "field2" : "second data",
  "field3" : 72
}

Example class is:

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

Note: Example class must have getter + setter methods for all fields

Upvotes: 1

Gabouchet
Gabouchet

Reputation: 191

jsonArray.add(evidenceObj);
// ...
evidenceObj.put("add", jsonArray);

You can't do that, there are a circular dependencie. If Java don't protect from that behavior, you will have a infinite loop or a stack overflow when you will export your json

Upvotes: 1

Related Questions