gpt4 book ai didi

java - 在 Java 中生成 2d Json 数组

转载 作者:行者123 更新时间:2023-12-02 03:03:26 25 4
gpt4 key购买 nike

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

Java 代码..

          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 以及消息“错误请求”。有没有办法保留元素的顺序?

最佳答案

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

请注意,我不确定您使用哪个库来表示 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);

关于java - 在 Java 中生成 2d Json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42107710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com