0

I'm new to Java but am developing a Minecraft plugin, I need to store some data and decided a json would be the easiest since I won't have easy access to a database for the plugin.

I'm attempting to make a basic JSON to (Multidimensional) Array so that I can save my data - however all merges end up appending themselves to the top level "AreaData" tab and not properly creating the array I'm looking for.

Below is parts of the code I'm trying - wondering if a HashMap is even a good idea for this purpose

import java.util.HashMap;
public class AreaHash {
 protected HashMap<String, String> AreaData = new HashMap<String, String>();
 public AreaHash(String name, String Owner, Integer X, Integer Y, Integer Z){
 AreaData.put("Name", name);
 AreaData.put("Owner", Owner);
 AreaData.put("X", X.toString());
 AreaData.put("Y", Y.toString());
 AreaData.put("Z", Z.toString());
 }
}
AreaHash RequestedArea = new AreaHash(Name, Owner.getUniqueId().toString(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
String jsonString = new Gson().toJson(RequestedArea);
DataControl.UpdateJson("raid-areas.json", jsonString);

JSON Output

{
 "AreaHash": {
 "Name": "Test",
 "Owner": "gold",
 "X": "10",
 "Y": "15",
 "Z": "-1000"
 }, 
}

What I'm actually looking to produce

{
 "1": [
 "Name": true,
 "Owner": "gold",
 "X": "10",
 "Y": "15",
 "Z": "-1000"
 ],
 "2": [
 "Name": true,
 "Owner": "silver",
 "X": "2110",
 "Y": "15",
 "Z": "-1200"
 ],
 "3": [
 "Name": true,
 "Owner": "test",
 "X": "-110",
 "Y": "70",
 "Z": "-1000"
 ],
}
asked Dec 7, 2020 at 11:21
1
  • How do you construct the object with multiple AreaHash objects? Commented Dec 7, 2020 at 12:29

1 Answer 1

1

You can create a Map of AreaHash objects then convert it to JSON string.

Map<String, AreaHash> data = new HashMap<>();
data.put("1", new AreaHash(...));
data.put("2", new AreaHash(...));
data.put("3", new AreaHash(...));
String jsonString = new Gson().toJson(data);
System.out.println(jsonString);

Output (Pretty printed)

{
 "1": {
 "Name": "Test",
 "Owner": "gold",
 "X": "10",
 "Y": "10",
 "Z": "0"
 },
 "2": {
 "Name": "Test",
 "Owner": "silver",
 "X": "10",
 "Y": "10",
 "Z": "-10"
 },
 "3": {
 "Name": "Test",
 "Owner": "test",
 "X": "10",
 "Y": "10",
 "Z": "-10"
 }
}

Also, take note that your desired output is not valid JSON.

[
 "Name": true,
 "Owner": "gold",
 "X": "10",
 "Y": "15",
 "Z": "-1000"
] // this will not parse
answered Dec 7, 2020 at 13:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.