pix*_*lds 19 java json jackson
我正在尝试在java中创建这个json对象并且苦苦挣扎:
{
"notification": {
"message": "test",
"sound": "sounds/alarmsound.wav",
"target": {
"apps": [
{
"id": "app_id",
"platforms": [
"ios"
]
}
]
}
},
"access_token": "access_token"
}
Run Code Online (Sandbox Code Playgroud)
任何人如何在java中创建它的帮助将不胜感激!
fge*_*fge 42
如果你真的想要创建 JSON对象,杰克逊有你想要的一切:
final JsonNodeFactory factory = JsonNodeFactory.instance;
final ObjectNode node = factory.objectNode();
final ObjectNode child = factory.objectNode(); // the child
child.put("message", "test");
// etc etc
// and then:
node.set("notification", child); // node.put(String, ObjectNode) has been deprecated
Run Code Online (Sandbox Code Playgroud)
结果node
是一个ObjectNode
继承的JsonNode
,这意味着你得到了所有JsonNode
的细节:
.toString()
代表;.get()
,.path()
- GSON没有相应的功能,特别是没有.path()
,因为它无法模拟缺少的节点);MissingNode
表示一个不存在的节点,并NullNode
表示JSON为null,所有这些都继承JsonNode
(GSON没有相应的 - 并且所有JsonNode
的导航方法也可以在这些节点上使用);.equals()
/ .hashCode()
.小智 23
对于那些试图使用此线程的答案的人,请注意
JsonNodeFactory nodeFactory = new JsonNodeFactory();
Run Code Online (Sandbox Code Playgroud)
使用包jackson-databind不再工作了.相反,你应该这样做
final JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
Run Code Online (Sandbox Code Playgroud)
请参阅此答案:https://stackoverflow.com/a/16974850/3824918
小智 9
JSONObject jsonComplex = new JSONObject();
JSONObject notification = new JSONObject();
notification.put("message", "test");
notification.put("sound", "sounds_alarmsound.wav");
JSONObject targetJsonObject= new JSONObject();
JSONArray targetJsonArray= new JSONArray();
JSONObject appsJsonObject= new JSONObject();
appsJsonObject.put("id","app_id");
JSONArray platformArray = new JSONArray();
platformArray.add("ios");
appsJsonObject.put("platforms",platformArray);
targetJsonArray.add(appsJsonObject);
targetJsonObject.put("apps", targetJsonArray);
notification.put("target", targetJsonObject);
jsonComplex.put("notification", notification);
jsonComplex.put("access_token", "access_token");
System.out.println(jsonComplex);
Run Code Online (Sandbox Code Playgroud)
感谢@fge,他为我提供了必要的信息来解决这个问题。
这就是我为解决这个问题所做的!
JsonNodeFactory nodeFactory = new JsonNodeFactory();
ObjectNode pushContent = nodeFactory.objectNode();
ObjectNode notification = nodeFactory.objectNode();
ObjectNode appsObj = nodeFactory.objectNode();
ObjectNode target = nodeFactory.objectNode();
ArrayNode apps = nodeFactory.arrayNode();
ArrayNode platforms = nodeFactory.arrayNode();
platforms.add("ios");
appsObj.put("id","app_id");
appsObj.put("platforms",platforms);
apps.add(appsObj);
notification.put("message",filledForm.field("text").value());
notification.put("sound","sounds/alarmsound.wav");
notification.put("target", target);
target.put("apps",apps);
pushContent.put("notification", notification);
pushContent.put("access_token","access_token");
if(!filledForm.field("usage").value().isEmpty()) {
target.put("usage",filledForm.field("usage").value());
}
if(!filledForm.field("latitude").value().isEmpty() && !filledForm.field("longitude").value().isEmpty() && !filledForm.field("radius").value().isEmpty()) {
target.put("latitude",filledForm.field("latitude").value());
target.put("longitude",filledForm.field("longitude").value());
target.put("radius",filledForm.field("radius").value());
}
Run Code Online (Sandbox Code Playgroud)
打印 pushContent 而不是输出我需要创建的确切 json 对象!
希望这也能帮助其他人!
富有的
归档时间: |
|
查看次数: |
41456 次 |
最近记录: |