简体   繁体   English

我想使用 JSONObject 和 JSONArray 构建一个类似于 Java 中的结构的 JSON 对象。 我该怎么做?

[英]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.这是 json。 Something similar to Creating nested JSON object for the following structure in Java using JSONObject?类似于使用 JSONObject 在 Java 中为以下结构创建嵌套的 JSON 对象? 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-您可以尝试使用JSONSimple library并以这种方式形成对象-

You have to import org.json.simple.JSONArray and org.json.simple.JSONObject for using this code.您必须导入org.json.simple.JSONArrayorg.json.simple.JSONObject才能使用此代码。

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

暂无
暂无

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

相关问题 我想使用org.json.simple.JSONObject生成以下JSON数据对象,如何在Java中做到这一点? - I want to generate following JSON dataobject using org.json.simple.JSONObject, how to do it in java? 如何在Java中检测嵌入式JSON元素是JSONObject还是JSONArray - How do I detect if an embedded JSON element is a JSONObject or JSONArray in Java Android-JSON-如何使用JSONObject和JSONArray类型创建JSON对象和JSON数组以匹配此格式? - Android - JSON - How do I create a JSON Object & JSON Array to match this format using JSONObject & JSONArray Types? 使用JSONObject在Java中为以下结构创建嵌套的JSON对象 - Creating nested JSON object for the following structure in Java using JSONObject org.json.JSONException:JSONObject [“ values”]不是JSONArray。” - org.json.JSONException: JSONObject[“values”] is not a JSONArray." 获取“ JSONObject [” myKey”]不是JSONArray。” - Getting “JSONObject[”myKey“] is not a JSONArray.” 在Gson中,如何将JsonArray添加到JsonObject? - In Gson, how do I add a JsonArray to a JsonObject? 如何将JSONArray转换为JSONObject? - How can I turn a JSONArray into a JSONObject? 我想用 JSONObject 在 JAVA 中的 Json Object 中添加一个新的 Object - I want to add a new Object inside a Json Object in JAVA with JSONObject 与邮递员相比,我在响应列表器中得到Empty Json Object。 我如何从JsonObject获取JsonArray作为响应? - I am getting Empty Json Object in response listner as compared to postman. How can i get JsonArray from JsonObject in response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM