如何在java中创建复杂的json对象?

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)

  • 下次添加一些解释是好的,而不仅仅是转储代码. (11认同)
  • 恕我直言,当代码清晰、不言自明和直观时,不需要以解释的名义冗长的额外文本行。让答案简短而简单。 (3认同)
  • 你是对的@Rishi,但在这种情况下,一位编码员问了一个与代码相关的问题。另一位编码员用一些代码回答了。你读过上面的代码了吗?它非常简单直观。所以在这里我认为不需要额外的解释。 (3认同)

pix*_*lds 5

感谢@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 对象!

希望这也能帮助其他人!

富有的