-2

I have the following JSON

{
        "display": {
            "icon": {
                "item": "minecraft:elytra"
            },
            "title": "Learn to Fly"
        },
        "parent": "minecraft:story/enter_end_gateway",
        "criteria": {
            "elytra": {
                "trigger": "minecraft:inventory_changed",
                "conditions": {
                    "items": [
                        {
                            "item": "minecraft:elytra",
                            "data": 1
                        }
                    ]
                }
            }
        }
    }

How would I be able to create this within Java?

Along with being able to get each element, eg the title.

Thanks!

Chazmondo
  • 37
  • 1
  • 7

5 Answers5

2

check this library gson from google its easy to use and have a lot of APIs to work with JSON

ROOT
  • 11,363
  • 5
  • 30
  • 45
2

The process you are asking about is called unmarshalling (parsing a serialised object in JSON/XML/other formats into an object to work with within the object-oriented context).

I would recommend having a look at Jackson. It's a popular library which is used in such the well-known frameworks like Spring (the MVC module). Having created a domain object, you could transform a jsonData into an instance of the DomainClass class like

DomainClass instance = new ObjectMapper().readValue(jsonData, DomainClass.class);

The jsonData source can be Files, InputStreams, byte arrays, Strings and so on. It's up to you to pick up the most convenient way to obtain that data.

Along with being able to get each element, eg the title.

When the instance is ready, you are free to get elements (now fields) through the accessors (the get methods).

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
1

Here is the code to build your json with org.json library in java:

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONBuilderMinecraft {

    public static void main(String args[]) throws Exception{

        JSONObject mainJson = new JSONObject();



        //inner most json array
        JSONArray itemarray=new JSONArray();

        //inner most json
        JSONObject itemsJson= new JSONObject();
        itemsJson.put("item", "minecraft:elytra");
        itemsJson.put("data", 1);
        itemarray.put(itemsJson);

        JSONObject conditions = new JSONObject();
        conditions.put("items", itemarray);


        JSONObject elytra = new JSONObject();
        elytra.put("trigger", "minecraft:inventory_changed");
        elytra.put("conditions", conditions);


        mainJson.put("criteria", elytra);
        mainJson.put("parent", "minecraft:story/enter_end_gateway");

        JSONObject icon = new JSONObject();
        icon.put("item", "minecraft:elytra");

        JSONObject display = new JSONObject();
        display.put("title", "Learn to Fly");
        display.put("icon", icon);

        mainJson.put("display", display);

        System.out.println(mainJson.toString());

    }


}
Tanmay Delhikar
  • 1,275
  • 11
  • 16
0

instead of creating your code like this stupid idea

JSONObject jsonObject=new JSONObject();
JSONObject jsonObject2=new JSONObject();
JSONObject jsonObject3=new JSONObject();

jsonObject.put("display",jsonObject3);

you can use Gson, so simple it creates json object without wasting your time on code make a class for each new Json object and make a relations between them.

Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
-1

Try this,it works fine:

    public static void main(String[] args) throws JSONException {
        String jsonString = "{\n"
                + "        \"display\": {\n"
                + "            \"icon\": {\n"
                + "                \"item\": \"minecraft:elytra\"\n"
                + "            },\n"
                + "            \"title\": \"Learn to Fly\"\n"
                + "        },\n"
                + "        \"parent\": \"minecraft:story/enter_end_gateway\",\n"
                + "        \"criteria\": {\n"
                + "            \"elytra\": {\n"
                + "                \"trigger\": \"minecraft:inventory_changed\",\n"
                + "                \"conditions\": {\n"
                + "                    \"items\": [\n"
                + "                        {\n"
                + "                            \"item\": \"minecraft:elytra\",\n"
                + "                            \"data\": 1\n"
                + "                        }\n"
                + "                    ]\n"
                + "                }\n"
                + "            }\n"
                + "        }\n"
                + "    }";

        JSONObject object = new JSONObject(jsonString);
        JSONObject disp = object.getJSONObject("display");
        String title = disp.getString("title");

        System.out.println("title is " + title);
    }
Young Emil
  • 2,220
  • 2
  • 26
  • 37