3

Okay so the output I would like to get is this:

  {
    "id": 460,
    "position": {
      "x": 3078,
      "y": 3251,
      "z": 0
    },
    "random-walk": true,
    "walk-radius": 1
  },

But the one I currently get is:

{
  "id": 460,
  "position": "{
    "x": 3078,
    "y": 3251,
    "z": 0
  }",
  "random-walk": true,
  "walk-radius": 0
},

The problem is the position object that I am trying to convert to json. The code I tried:

Path path = Paths.get("./npcs.json");
File file = path.toFile();
file.getParentFile().setWritable(true);

if (!file.getParentFile().exists()) {
    try {
        file.getParentFile().mkdirs();
    } catch (SecurityException e) {
        System.out.println("Unable to create directory for donator data!");
    }
}

try (FileWriter writer = new FileWriter(file)) {

    Gson builder = new GsonBuilder().setPrettyPrinting().create();
    JsonObject object = new JsonObject();

    Position pos = new Position(mob.absX, mob.absY, mob.heightLevel);
    object.addProperty("id", mob.npcId);
    object.addProperty("position", builder.toJson(pos));
    object.addProperty("random-walk", mob.randomWalk);
    object.addProperty("walk-radius", mob.walkingType);

    writer.write(builder.toJson(object));
    writer.close();

} catch (Exception e) {
    System.out.println("Something went wrong with saving for mob !");
    e.printStackTrace();
}

Does anyone has a clue on how to get the first result? So without the double-quotes.

2
  • 1
    toJson is going to return JSON, not an object. Commented Feb 12, 2016 at 16:54
  • @DaveNewton I see, could you tell me how to do this properly then? Commented Feb 12, 2016 at 17:03

3 Answers 3

2

Use this

object.add("position", new Gson().toJsonTree(pos));

instead of

object.addProperty("position", builder.toJson(pos));

result should than look like this:

"position": {
    "x": 10,
    "y": 50
  },
1
  • Thanks, this is what I was looking for! Commented Feb 12, 2016 at 17:19
0
JSONObject json = new JSONObject();
JSONArray addresses = new JSONArray();
JSONObject address;
try
{
   int count = 15;

   for (int i=0 ; i<count ; i++)
   {
       address = new JSONObject();
       address.put("Name","Name no." + i);
       address.put("Country", "Country no." + i);
       addresses.put(address);
   }
   json.put("Addresses", addresses);
}
catch (JSONException jse)
{
    out.println("Error during json formatting" + jse.getMessage());
}

I would recommend using a JSONObject for your main JSON. After that, add each of the components. For a vector, add a json array. Here is a simple example I used to understand this better.

0

You can use your own java objects to be precise. Gson accesses the fields in your class using reflection, so you won't have to parse anything manually.

For example in your case:

    import com.google.gson.annotations.SerializedName;

    public class Walk {
        private int id;
        private Position position;

        @SerializedName("random-walk")
        private boolean randomWalk;

        @SerializedName("walk-radius")
        private int walkRadius;
    }
    public class Position {
        private int x,y,z;
    }

Then use

 Gson gson = new Gson();
 Walk walk = gson.fromJson(yourJson, Walk.class);
2
  • I want to write my java object to json and not convert my json to a java object. Thanks though :) Commented Feb 12, 2016 at 17:08
  • Yes, you can do the reverse as well. Just call toJson. You may need to alter your Java objects a bit, but that will be the proper way of doing it. Commented Feb 12, 2016 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.