在Java

时间:2017-02-08 08:06:27

标签: java arrays json

我正在尝试使用Java中的JSON对象生成2d JSON数组。我正在尝试生成以下JSON。

Java Code ..

          JSONObject root = new JSONObject();
        JSONObject c0 = new JSONObject();
        JSONObject c1 = new JSONObject();

        JSONObject attachment = new JSONObject();
        JSONObject payload = new JSONObject();
        JSONObject buttons = new JSONObject();

        root.put("recipient", c0);
        root.put("message", c1);

        c0.put("id", userId);
        c1.put("message", attachment);

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

        payload.put("template_type", "button");
        payload.put("text", "What do you want to do next");
        payload.put("buttons", buttons);

        buttons.put("type", "web_url");
        buttons.put("url", "https://google.com");
        buttons.put("title", "show website");

        buttons.put("type", "postback");
        buttons.put("title", "Hi There");
        buttons.put("payload", "sample payload");

预期的JSON输出 ..

{
"recipient":{
"id":"USER_ID"
 },
"message":{
"attachment":{
  "type":"template",
  "payload":{
    "template_type":"button",
    "text":"What do you want to do next?",
    "buttons":[
      {
        "type":"web_url",
        "url":"https://google.com",
        "title":"Show Website"
      },
      {
        "type":"postback",
        "title":"Start Chatting",
        "payload":"Sample_PAYLOAD"
      }
     ]
    }
   }
 }
}

当前输出 ..

 {
"recipient":{"
id":"988459377921053"
},
"message":{
"message":{"
  payload":{
   "buttons":{
       "payload":"sample payload",
       "type":"postback",
        "title":"Hi There",
        "url":"https://google.com"
         },
      "template_type":"button",
      "text":"What do you want to do next"},
       "type":"template"
       }
      }
     }

我正在创建嵌套的Json对象并将它们从外层添加到内层仍然输出JSON不是预期的。无法理解我哪里出错了。

修改1:

尝试用户@ user1802604提到的更改,但生成的JSON格式如下..

{  
"recipient":{  
  "id":"988459377921053"
},
"message":{  
  "attachment":{  
     "payload":{  
        "buttons":[  
           {  
              "payload":"sample payload",
              "type":"postback",
              "title":"Hi There",
              "url":"https://google.com"
           },
           {  
              "payload":"sample payload",
              "type":"postback",
              "title":"Hi There",
              "url":"https://google.com"
           }
        ],
        "template_type":"button",
        "text":"What do you want to do next"
     },
     "type":"template"
     }
    }
   }

我发送JSON的API返回响应代码400,消息“Bad Request”。有没有办法保持元素的顺序?

2 个答案:

答案 0 :(得分:2)

我认为你的大部分代码都是正确的。只需修复两个错误。

请注意,我不确定您用于表示json的库,因此以下代码可能无法完全正确。我假设你正在使用org.json。

  1. c1.put("message", attachment);应为c1.put("attachment", attachment);
  2. JSONObject buttons = new JSONObject();应创建为JSONArray buttons = new JSONArray();。因此,您还必须创建另一个json对象来存储typetitleurl

    JSONArray buttons = new JSONArray();
    
    JSONObject button1 = new JSONObject();
    button1.put("type", "web_url");
    button1.put("url", "https://google.com");
    button1.put("title", "show website");
    buttons.put(button1);
    
    JSONObject button2 = new JSONObject();
    button2.put("payload", "postback");
    button2.put("url", "Sample_PAYLOAD");
    button2.put("title", "Start Chatting");
    buttons.put(button2);
    

答案 1 :(得分:0)

此代码是JSONObject

的设置值
    buttons.put("type", "web_url");
    buttons.put("url", "https://google.com");
    buttons.put("title", "show website");

    buttons.put("type", "postback");
    buttons.put("title", "Hi There");
    buttons.put("payload", "sample payload");

由于每个键有两次,因此您将覆盖第一个值。

您想要的是将JSONObject个实例添加到JSONArray buttons

JSONArray arrayButton= new JSONArray();

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

button.put("type", "postback");
button.put("title", "Hi There");
button.put("payload", "sample payload");
arrayButton.put(button);

将每个对象添加到数组中。然后只需添加您构建的数组。