gpt4 book ai didi

android - 如何使用 JsonArray 和 JsonObject 创建 Json

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:19 29 4
gpt4 key购买 nike

我想创建一个 Json 结构,它实际上是 JsonObject 中的 JsonArray。示例结构为:

1.

{
“req”: [
{
“ctrlId”:”txt1”
},
{
“ctrlId”:”txt2”
}
]
}

2.

{
“req”: [
{
“ctrlId”:”txt1”,
“val” : “val1”
},
{
“ctrlId”:”txt2”,
“val” : “val2”
}
]
}

但我无法得到它..感谢任何帮助..

最佳答案

    JSONObject obj = new JSONObject();
JSONArray req = new JSONArray();

JSONObject reqObj = new JSONObject()
reqObj.put( "ctrlId", "txt1" );
req.put( reqObj );
reqObj = new JSONObject();
reqObj.put( "ctrlId", "txt2" );
req.put( reqObj );

obj.put( "req", req );

最终对象是obj

关于android - 如何使用 JsonArray 和 JsonObject 创建 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11260715/

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