3 回答

TA贡献1813条经验 获得超2个赞
您似乎试图在 json 中添加太多值,并且签名中没有包含三个参数的方法。我认为您的代码中有错误。
put("name",("Luis"), new JsonObject().put("phoneNumber",("526677777777")))
这肯定是有错误的("Luis")
也许应该是这样的:
JsonObject person = new JsonObject();
person.put(new JsonObject("name", "Luis"));
person.put(new JsonObject("phoneNumber", "526677777777"));

TA贡献1880条经验 获得超4个赞
根据您的示例JSON
,应该只有 2 个 JSON 对象,而不是三个,内部JSON
对象包含name
和phoneNumber
。
它还有助于在单独的行上格式化内容,试试这个:
yourInitialObject.put("person",new JsonObject() .put("name","Luis") .put("phoneNumber","526677777777"));

TA贡献1847条经验 获得超7个赞
下面的代码应该给出您期望的 JSON 结构,
//create a json object to hold the json elements
JsonObject jsonElements = new JsonObject();
jsonElements.put("name","Luis");
jsonElements.put("phoneNumber","526677777777");
//now create another json object to hold the json element created
JsonObject jsonMain = new JsonObject();
jsonMain.put("person",jsonElements);
添加回答
举报