构建一个嵌套的JSONObject [英] Building a nested JSONObject

查看:123
本文介绍了构建一个嵌套的JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我正在使用的代码

Below is the code I am using

    JSONObject requestParams = new JSONObject();

    requestParams.put("something", "something value");
    requestParams.put("another.child", "child value");

这是需要发布API的方式

This is how the API needs to be posted

{
   "something":"something value",
   "another": {
   "child": "child value"
   }
}

我收到一条错误消息,指出需要another.child字段."

I get an error stating that "The another.child field is required."

如何通过restAssured发布此内容?其他不需要通过嵌套工作进行发布的API,因此我假设这就是失败的原因.

How do I go about posting this via restAssured? The other APIs that do not require posting with nesting work, so I'm assuming that's why it's failing.

推荐答案

您发布的内容是因为JSONObject没有点分隔的键路径的概念.

What you posted was this since JSONObject has no notion of dot-separated key paths.

{
   "something":"something value",
   "another.child": "child value"
}

您需要再创建一个JSONObject

JSONObject childJSON = new JSONObject():
childJSON.put("child", "child value");
requestParams.put("another", childJSON);

这篇关于构建一个嵌套的JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆