gpt4 book ai didi

android - 将 key 添加到 jsonObject

转载 作者:行者123 更新时间:2023-11-30 00:42:19 25 4
gpt4 key购买 nike

我想做这样的事情:

data{"user":{"age":"23","address":"usa"}}

但我继续使用 JsonObject 构建它

{"data":{"user":{"age":"23","address":"usa"}}}

我知道,因为数据是一个 JsonObject。我只是不知道要让它像我第一次发布的那样。请帮忙

最佳答案

要实现您想要的输出,请尝试以下代码:

    try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("age", "23");
jsonObject.put("address", "usa");

JSONObject userJson = new JSONObject();
userJson.put("user", jsonObject);

String yourFormat = "data" + userJson.toString();
Log.d("TAG", "Format: " + yourFormat);
} catch (JSONException e) {
e.printStackTrace();
}

关于android - 将 key 添加到 jsonObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42359613/

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