1.14.4 Load JSON list

Discussion in 'Spigot Plugin Development' started by Matzuake, Feb 8, 2020.

  1. Hello
    I'm just trying to understand Json what is really hard for me right now

    Maybe someone can help me

    I'm trying to read this json
    Code (Text):
    {
      "holz": [
        {
          "itemname": "Eichenholz",
          "item": {
            "material": "OAK_LOG",
            "anzahl": "1"
          },
          "lore": [
            {
              "preis": "10"
            }
          ]
        },
        {
          "itemname": "Brauner Pilzblock",
          "item": {
            "material": "BROWN_MUSHROOM_BLOCK",
            "anzahl": "1"
          },
          "lore": [
            {
              "preis": "15"
            }
          ]
        },
        {
          "itemname": "Tropenbaumlaub",
          "item": {
            "material": "JUNGLE_LEAVES",
            "anzahl": "1"
          },
          "lore": [
            {
              "preis": "2"
            }
          ]
        },
        {
          "itemname": "Eichenholz",
          "item": {
            "material": "OAK_LOG",
            "anzahl": "64"
          },
          "lore": [
            {
              "preis": "64"
            }
          ]
        },
        {
          "itemname": "Eichenholz",
          "item": {
            "material": "OAK_LOG",
            "anzahl": "64"
          },
          "lore": [
            {
              "preis": "10"
            }
          ]
        },
        {
          "itemname": "Brauner Pilzblock",
          "item": {
            "material": "BROWN_MUSHROOM_BLOCK",
            "anzahl": "64"
          },
          "lore": [
            {
              "preis": "960"
            }
          ]
        },
        {
          "itemname": "Tropenbaumlaub",
          "item": {
            "material": "JUNGLE_LEAVES",
            "anzahl": "64"
          },
          "lore": [
            {
              "preis": "128"
            }
          ]
        }
      ]
    }

    Code (Java):


    Gson gson = new Gson();
    ShopItems[] shopItems= gson.fromJson(json, ShopItems[].class);
    for(ShopItems shopItem: shopItems)
        System.out.println(shopItem.toString());
     
    Unfortunately, he doesn't load them as I thought

    but always a nullpointer
     
  2. Post the error. And what is the value of 'json'?
     
  3. Code (Text):
    [20:24:11] [Server thread/WARN]: java.lang.NullPointerException
    [20:24:11] [Server thread/WARN]:     at net.letsplaymc.spawnshop.main.LetsPlayMcSpawnShop.setupItems(LetsPlayMcSpawnShop.java:139)
    [20:24:11] [Server thread/WARN]:     at net.letsplaymc.spawnshop.ShopInventare.Shop1inv.holz(Shop1inv.java:30)
    [20:24:11] [Server thread/WARN]:     at net.letsplaymc.spawnshop.ShopInteract.Shop1Interact.onClick(Shop1Interact.java:64)
    [20:24:11] [Server thread/WARN]:     at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor92.execute(Unknown Source)
    [20:24:11] [Server thread/WARN]:     at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69)
    [20:24:11] [Server thread/WARN]:     at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80)
    [20:24:11] [Server thread/WARN]:     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70)
    [20:24:11] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:545)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:2310)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:33)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:10)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:1013)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:1006)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:990)
    [20:24:11] [Server thread/WARN]:     at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:923)
    [20:24:11] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
    [20:50:31] [Server thread/WARN]: java.lang.NullPointerException
     


    for 1 item
    is easy

    Code (Java):
     try {
                    JSONParser jsonParser = new JSONParser();
                    Object parsed = jsonParser.parse(new FileReader(file.getPath()));
                    JSONObject jsonObject = (JSONObject) parsed;
                    String itemname = (String) jsonObject.get("itemname");

                    JSONObject itemObject = (JSONObject) jsonObject.get("item");
                    String itemType = (String) itemObject.get("id");

                    ItemStack itemStack = new ItemStack(Material.valueOf(itemType.toUpperCase()));
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    ArrayList<String> lore = new ArrayList<>();

                    JSONArray loreArray = (JSONArray) jsonObject.get("lore");

                    for (Object lorelines : loreArray) {
                        JSONObject loredata = (JSONObject) lorelines;
                        String lore1 = (String) loredata.get("lore1");
                        String lore2 = (String) loredata.get("lore2");
                        String lore3 = (String) loredata.get("lore3");

                        lore.add(lore1);
                        lore.add(lore2);
                        lore.add(lore3);
                    }

                    itemMeta.setLore(lore);
                    itemMeta.setDisplayName(ChatColor.LIGHT_PURPLE + itemname);
                    itemStack.setItemMeta(itemMeta);

                    player.getInventory().addItem(itemStack);


                } catch (ParseException | IOException e) {
                    e.printStackTrace();
                }
     
    #3 Matzuake, Feb 8, 2020
    Last edited: Feb 8, 2020