0

JAVA

I want to parse every element of JSON file (using gson will be the best) to Array. I searched for two days and I still can't figure out how to do it correctly. My JSON file looks like this:

{
  "spawn1": {
    "x": 336.4962312645427,
    "y": 81.0,
    "z": -259.029426052796
  },
  "spawn2": {
    "x": 341.11558917719424,
    "y": 80.0,
    "z": -246.07415114625
  }
}

There will be more and more elements with different names. What I need is to take all the elements like x, y, z and create an object using it and place it to the array. Class for this object looks like this:

public class Chest {
  private double x, y, z;

  public Chest(double x, double y, double z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }
}
7
  • It would be better if you specify what programming language you are using? Is it C#? Commented Mar 21, 2018 at 12:17
  • OMG! I'm so sorry. Just edited - It's Java :) Commented Mar 21, 2018 at 12:22
  • Your data is a JSON object with keys "spawn1" and "spawn2". How do you want it to be converted? An array of Chest objects ignoring the keys? Commented Mar 21, 2018 at 12:24
  • Umm. Yea, I don't need those names in my object. The only thing I will be using are coordinates. Commented Mar 21, 2018 at 12:27
  • 1
    You know GSON but don't have tried to implement this, why ? Commented Mar 21, 2018 at 12:34

2 Answers 2

1

With GSON

Using the JSON you have is a bit tricky since you said the number of Object can change but those are not in an array. But you can iterate each element with GSON using JsonObject.entrySet

First, parse the JSON and get the object:

JsonObject json = JsonParser.parse(jsonToParse).getAsJsonObject();

Then you iterate the elements :

List<Chest> list = new ArrayList<>();
for(Entry<String, JsonElement> e : json.entrySet()){
    //read the json you can find in `e.getValue()`
    JsonObject o = e.getValue().getAsJsonObject();
    double x = o.getAsJsonPrimitive("x").getAsDouble();
    ...

    //create the instance `Chest` with those `double` and insert into a `List<Chest>`
    list.add(new Chest(x,y,z));
}

If you want to retrieve the name to, it is in the entry key : e.getKey().

Note this can be improved using a Stream but to keep it family friendly ;) I will keep that loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will check that tommorow
1

With the class you are currently using it is not going to work.

Your JSON String currently wants 2 classes that look like this:

public class ClassOne
{
    ClassTwo spawn1;
    ClassTwo spawn2;
}

public class ClassTwo
{
    double x;
    double y;
    double z;
}

So you need to either change your JSON or your class structure.


edit

If you want the class you are currently using you need a JSON string of this form:

[
  {
    "x": 1.0,
    "y": 2.0,
    "z": 3.0
  },
  {
    "x": 4.0,
    "y": 5.0,
    "z": 6.0
  }
]

If you want to maintain your spawn1 and spawn2 field add a String field to your class and use a JSON String like this (here the field has the name name):

[
  {
    "name": "spawn1",
    "x": 1.0,
    "y": 2.0,
    "z": 3.0
  },
  {
    "name": "spawn2",
    "x": 4.0,
    "y": 5.0,
    "z": 6.0
  }
]

Both of these return an Chest[] when you convert them from JSON.

4 Comments

"What I need is to take all the elements like x, y, z and create an object using it and place it to the array"
@AxelH I totally understand that this is what he wants but this is not what his JSON is corresponding to. From the JSON String he has he will not ever get that without a custom loader. So "either change your JSON or your class structure".
But as I said, there will be more objects like spawn1 and spawn2 so creating class for each one is not very good idea, I think. Is it impossible iterate through every object like "spawn1", "spawn2", create object for each of them and put it into array of Chest objects?
@Norbox I've updated my answer giving you a huge nod in the correct direction. Generally if your JSON string does not deserialize to your object try it the other way around: Serialize one of your objects to JSON to see how the String is supposed to look like.

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.