简体   繁体   中英

I want to build a JSON Object similar to following the structure in java using JSONObject and JSONArray. How can I do it?

This is the json. Something similar to Creating nested JSON object for the following structure in Java using JSONObject? will help.

{
      "taskAssings": [
        {
          "taskAssigned": {
            "id": "3c814009-82f7-4246-bc51-2d263e758561"
          },
          "taskAssignee": {
            "id": "3c814009-82f7-4246-bc51-2d263e758561"
          }
        }
      ],
      "description": "TestTaskDescription",
      "assignTo": {
        "id": "3c814009-82f7-4246-bc51-2d263e758561"
      },
      "name": "taskname",
      "status": {
        "id": "7d8a0d80-5c93-46cc-982d-47399503beaa"
      },
      "priority": {
        "id": "842a9a9c-4a1a-4f70-bf4d-8181b482f651"
      }
    }

You can try with JSONSimple library and form the object in this way-

You have to import org.json.simple.JSONArray and org.json.simple.JSONObject for using this code.

    JSONObject object=new JSONObject();

    JSONObject holder=new JSONObject();
    JSONArray taskAssings = new JSONArray();

    JSONObject taskAssigned=new JSONObject();
    taskAssigned.put("id", "3c814009-82f7-4246-bc51-2d263e758561");

    JSONObject taskAssignee=new JSONObject();
    taskAssignee.put("id", "3c814009-82f7-4246-bc51-2d263e758561");

    holder.put("taskAssigned",taskAssigned);
    holder.put("taskAssignee",taskAssignee);
    taskAssings.add(holder);

    object.put("taskAssings", taskAssings);

    JSONObject status=new JSONObject();
    status.put("id", "7d8a0d80-5c93-46cc-982d-47399503beaa");
    object.put("status", status);

    JSONObject assignTo=new JSONObject();
    assignTo.put("id", "3c814009-82f7-4246-bc51-2d263e758561");
    object.put("assignTo", assignTo);


    JSONObject priority=new JSONObject();
    priority.put("id", "842a9a9c-4a1a-4f70-bf4d-8181b482f651");
    object.put("priority",priority);

    object.put("description", "TestTaskDescription");
    object.put("name", "taskname");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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