json - 如何使用 curl 将 json 对象与数组一起放置

标签 json curl

我有一系列数据要输入数据库。输入数据的用户界面不适合批量输入,所以我正在尝试制定一个等效的命令行。当我在 chrome 中检查 UI 的网络请求时,我看到了一个 json 对象的 PUT 请求。当我尝试复制请求时

curl -H 'Accept: application/json' -X PUT '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`

我收到一个错误

curl: (3) [globbing] nested braces not supported at pos X

其中 X 是第一个“[”的字符位置。

如何放置一个包含数组的 json 对象?

最佳答案

你的命令行应该有 -d/--data插入到要在 PUT 中发送的字符串之前,并且要设置 Content-Type 而不是 Accept。

curl -H 'Content-Type: application/json' -X PUT -d '[JSON]' \
     http://example.com/service

使用问题中的确切 JSON 数据,完整的命令行将变为:

curl -H 'Content-Type: application/json' -X PUT \
    -d '{"tags":["tag1","tag2"],
         "question":"Which band?",
         "answers":[{"id":"a0","answer":"Answer1"},
                    {"id":"a1","answer":"answer2"}]}' \
    http://example.com/service

注意:JSON 数据仅为可读性而包装,对 curl 请求无效。

关于json - 如何使用 curl 将 json 对象与数组一起放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15425446/

相关文章:

java - 如何在 Java 中使用 cURL?

swift - 等效的 Swift NSURLSession 或 Alamofire Curl 帖子

php - 从 URL 获取重定向的 URL

javascript - 是否可以使用 javascript 解析 JSON?

java - 如何使用 Apache Camel 处理大的 JSON 数组

Java:Gson更改属性名称而不序列化

elasticsearch - Elasticsearch查询问题-[范围]格式错误的查询,预期[END_OBJECT],但发现[FIELD_NAME]

javascript - 循环遍历json对象并将其放入html表中

json - 类似于 NodeJS 的 Typesafe Config

bash - 我可以返回并更改之前在 Bash 中重复的一行吗?