2D JSON数组不是在正确的Java中生成的

时间:2017-02-08 10:56:13

标签: java arrays json

我正在尝试使用Json Object,JSON Array在Java中生成2d JSON数组。生成的2d数组有效,但元素的排序错误。

Java代码...

        JSONObject root1 = new JSONObject();
        JSONObject c01 = new JSONObject();
        JSONObject c11 = new JSONObject();

        JSONObject attachment = new JSONObject();
        JSONObject payload = new JSONObject();
        JSONArray arrayButton= new JSONArray();
        JSONArray arrayelements= new JSONArray();
        JSONObject elementsObj = new JSONObject();
        JSONObject defaultAction = new JSONObject();

        root1.put("recipient", c01);
        root1.put("message", c11);

        c01.put("id", userId);
        c11.put("attachment", attachment);

        attachment.put("type", "template");
        attachment.put("payload", payload);

        payload.put("template_type", "generic");
        payload.put("elements", arrayelements);

        arrayelements.put(elementsObj);

        elementsObj.put("title", "Sample Title");
        elementsObj.put("image_url", "https://google.com/");
        elementsObj.put("subtitle", "Sample Sub Title");
        elementsObj.put("default_action", defaultAction);

            defaultAction.put("type", "web_url");
            defaultAction.put("url", "https://www.google.com/");
            defaultAction.put("messenger_extensions", "true");
            defaultAction.put("webview_height_ratio", "tall");
            defaultAction.put("fallback_url", "https://www.google.com/");

        elementsObj.put("buttons", arrayButton);

        JSONObject buttons1 = new JSONObject();
        buttons1.put("type", "web_url");
        buttons1.put("url", "https://google.com");
        buttons1.put("title", "show website");
        arrayButton.put(buttons1);

        JSONObject buttons2 = new JSONObject();
        buttons2.put("type", "postback");
        buttons2.put("title", "Hi There");
        buttons2.put("payload", "sample payload");
        arrayButton.put(buttons2);

预期输出

    {
    "recipient":{
    "id":"USER_ID"
      },
     "message":{
"attachment":{
  "type":"template",
  "payload":{
    "template_type":"generic",
    "elements":[
       {
        "title":"Sample title",
        "image_url":"https://google.com/company_image.png",
        "subtitle":"We\'ve got the right hat for everyone.",
        "default_action": {
          "type": "web_url",
          "url": "https://google.com/",
          "messenger_extensions": true,
          "webview_height_ratio": "tall",
          "fallback_url": "https://google.com/"
        },
        "buttons":[
          {
            "type":"web_url",
            "url":"https://google.com",
            "title":"View Website"
          },{
            "type":"postback",
            "title":"Start Chatting",
            "payload":"Sample payload"
          }              
        ]      
      }
    ]
    }
   }
  }
 }

当前输出

       {  
       "recipient":{  
       "id":"988459377921053"
        },
       "message":{  
  "attachment":{  
     "payload":{  
        "elements":[  
           {  
              "buttons":[  
                 {  
                    "type":"web_url",
                    "title":"show website",
                    "url":"https://google.com"
                 },
                 {  
                    "payload":"sample payload",
                    "type":"postback",
                    "title":"Hi There"
                 }
              ],
              "image_url":"https://s3-ap-southeast-1.amazonaws.com/fls-items-dev/sample-item-4-95/post/sample-item-4-95-primary-4495.png",
              "subtitle":"Sample Sub Title",
              "title":"Sample Title",
              "default_action":{  
                 "fallback_url":"https://www.frrndlease.com/",
                 "webview_height_ratio":"tall",
                 "messenger_extensions":"true",
                 "type":"web_url",
                 "url":"https://www.frrndlease.com/ItemDetails?uid=wilson-kid-235"
              }
           }
        ],
        "template_type":"generic"
     },
     "type":"template"
     }
    }
  }

按钮数组的顺序,对象template_type&类型是反向的。我正在创建嵌套的Json对象并将它们从外层添加到内层仍然输出JSON不是预期的。无法理解我哪里出错了。

0 个答案:

没有答案