简体   繁体   中英

How do I create a complex json object in java?

I'm trying to create this json object in java and struggling:

{
    "notification": {
        "message": "test",
        "sound": "sounds/alarmsound.wav",
        "target": {
            "apps": [
                {
                    "id": "app_id",
                    "platforms": [
                        "ios"
                    ]
                }
            ]
        }
    },
    "access_token": "access_token"
}

Any assistance in how someone would create that in java would be appreciated!

If you really are looking into creating JSON objects, Jackson has all you want:

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

The resulting node is an ObjectNode , which inherits JsonNode , which means you get all of JsonNode 's niceties:

  • a sensible .toString() representation;
  • navigation capabilities ( .get() , .path() -- GSON has no equivalent for that, in particular not .path() , since it cannot model a node that is missing);
  • MissingNode to represent a node that isn't there, and NullNode to represent JSON null, all of which inherit JsonNode (GSON has no equivalent for that -- and all of JsonNode 's navigation methods are also available on such nodes);
  • and of course .equals() / .hashCode() .

For those who try to use the answers of this thread, note that

JsonNodeFactory nodeFactory = new JsonNodeFactory();

Is not working anymore with the package jackson-databind. Instead you should do

final JsonNodeFactory nodeFactory = JsonNodeFactory.instance;

See this answer: https://stackoverflow.com/a/16974850/3824918

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);

Thanks go to @fge who provided the necessary information for me to solve this.

Here's what I did to solve this problem!

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());
        }

Printing pushContent than outputs the exact json object I needed to create!

Hope this helps someone else out there too!

Rich

I'd rather create classes representing that object, and convert it into JSON. I presume you got that JSON interface elsewhere and is lot on how to create it from Java.

class ToJson{
    private Notification notification;
    private String access_token;
}

public class Notification{
    private String message;
    private String sound;
    private Target target;
}

public class Target{
    private Apps apps[];
}

public class Apps{
    private String id;
    private Plataform plataforms[];
}

public class Plataform{
    privte String ios;
}

Then you convert a filled ToJson object to JSON using any lib/framework you like.

There are a variety of libraries that can help you here.

If you run into more specific problems with these libraries, you should post them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM