我的Json
正在关注
{
"q": {
"region": "NYC",
"or": [
{
"duration": "12"
}
]
},
"sort": "recent"
}
如何在JsonObject
中书写。我尝试使用以下代码:
JSONObject obj = new JSONObject();
JSONObject obj1 = new JSONObject();
JSONObject obj2 = new JSONObject();
JSONObject obj4 = new JSONObject();
try {
obj.put("q", obj1);
obj.put("sort", "recent");
obj1.put("reg", "NYC");
obj2.put("duration", new Integer(12));
obj4.put()
} catch (JSONException e) {
}
我无法创建列表数组。我怎样才能转换成Json对象?
答案 0 :(得分:0)
您可以用这种方式构建JSONObject:
public class Test {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.put("q", new JSONObject()
.put("region", "NYC")
.put("or", new JSONArray()
.put(new JSONObject()
.put("duration","12"))))
.put("sort", "recent");
System.out.println(obj);
}
}
输出:
{"q":{"or":[{"duration":"12"}],"region":"NYC"},"sort":"recent"}