[英]add JSONArray within a JSONObject
I'm making an application to send notifications using OneSignal and must do a POST request in JSON format. 我正在创建一个使用OneSignal发送通知的应用程序,并且必须以JSON格式执行POST请求。
To send notification to a user I have to use the include_player_ids
argument which have to be an array because it is possible to send to multiple users the same notification (in my case I only send notification to one user). 要向用户发送通知,我必须使用必须是数组的
include_player_ids
参数,因为可以向多个用户发送相同的通知(在我的情况下,我只向一个用户发送通知)。 I use a JSONArray to create this array but when adding it to the JSONObject, there are extra quotes for the field include_player_ids
. 我使用JSONArray创建此数组,但在将其添加到JSONObject时,字段
include_player_ids
还有额外的引号。
What I have: 是)我有的:
{
"headings": {"en":"my_title"},
"contents": {"en":"my_text"},
"include_player_ids": "[\"my-player-id\"]",
"app_id":"my-app-id"
}
As you can see, there is some quote around the array [ ]. 如您所见,数组[]周围有一些引用。
I guess it's what is making the response error from OneSignal : errors":["include_player_ids must be an array"]
我猜这是OneSignal的响应错误:
errors":["include_player_ids must be an array"]
What I want : 我想要的是 :
...
"include_player_ids": ["my-player-id"]
...
It is weird because adding JSONObject to JSONObject doesn't do this even though it is quite similar as you can see with the headings / contents fields 这很奇怪,因为将JSONObject添加到JSONObject不会这样做,即使它与标题/内容字段中看到的非常相似
My code : 我的代码:
import org.json.JSONException;
import org.json.JSONObject;
import org.json.alt.JSONArray;
JSONObject headings = new JSONObject();
JSONObject contents = new JSONObject();
JSONArray player_id = new JSONArray();
JSONObject notification = new JSONObject();
try {
notification.put("app_id", appId);
notification.put("include_player_ids", player_id);
player_id.put(idUser);
headings.put("en", "my_title");
contents.put("en", "my_text");
notification.put("headings", headings);
notification.put("contents", contents);
} catch (JSONException e) {
System.out.println("JSONException :" + e.getMessage());
}
idUser
is a String idUser
是一个String
Thanks in advance for any help, 在此先感谢您的帮助,
I believe the problem is that you're using org.json.alt.JSONArray
instead of org.json.JSONArray
. 我相信问题是你使用的是
org.json.alt.JSONArray
而不是org.json.JSONArray
。 I'm not familiar with that class, but I suspect JSONObject.put
is just calling toString()
on it rather than treating it as an existing JSON array. 我不熟悉该类,但我怀疑
JSONObject.put
只是在其上调用toString()
而不是将其视为现有的JSON数组。 Here's a short but complete example that doesn't have the problem: 这是一个简短而完整的示例, 没有问题:
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray; // Note the import here
public class Test {
public static void main(String[] args) throws JSONException {
JSONArray playerIds = new JSONArray();
playerIds.put("a");
playerIds.put("b");
JSONObject notification = new JSONObject();
notification.put("include_player_ids", playerIds);
System.out.println(notification);
}
}
Output: 输出:
{"include_player_ids":["a","b"]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.