-2

Been trying to convert an object in JSON to java using Gson but many things do not match up. Before the object was an int, now it's converted into an object. This is part of the JSON (I need only damage to get converted):

I've tried most of the guides online but still not helpful at all.

[
  {
    "username": "lol",
    "value": [
      "one","two"
    ],
    "Power": [
      {
        "type": "Damage",
        "damage": {"first":2, "second":0,"third":0}
}]}]
Lol
  • 77
  • 1
  • 1
  • 9
  • Can you provide the code you used to parse the JSON. Also, class file are not JSON so I don't understand what you tried to do with it. – AxelH May 28 '19 at 10:51
  • 1
    possible duplicate of https://stackoverflow.com/questions/2779251/how-can-i-convert-json-to-a-hashmap-using-gson – Sunchezz May 28 '19 at 10:52
  • and of cause, there are missing closing `]}]` brackets at the end of the json – Sunchezz May 28 '19 at 10:54
  • @AxelH I'm trying to parse the jSON type called "damage" as a hashmap in java. – Lol May 28 '19 at 11:00
  • @Sunchezz it's not a duplicate. It's way different.Moreover I don't know how to access "damage" and what type is it, jSONObject or jSONElement? – Lol May 28 '19 at 11:01
  • The Library Gson converts full text into Java objects ( where you have to name the class before). If you only want to access a certain part of the json, you have to options. Shrink the text before you are parsing it, or parse the full text and then access the type which you need. Therefore your string needs to be correct Json. – Sunchezz May 28 '19 at 11:05

2 Answers2

0

The JSON you are trying to parse seems not a valid Json structure. Use this https://jsonformatter.curiousconcept.com/ to validate the JSON structure and try parsing.

  • The jSON works perfectly. There were forgotten brackets at the end,just because what I posted here was the first element.Added the brqackets – Lol May 28 '19 at 11:02
0

To convert your Text into an Hashmap, use this.

String yourJson = "insert your JSON here";
Type type = new TypeToken<List<Map<String, Object>>>() {}.getType();
List<Map<String, Object>> parsedJson = new Gson().fromJson(yourJson, type);
        

now you can access it via:

Map<String, Object> user = (Map<String, Object>) parsedJson.get(0);
List<Map<String, Object>> userPowers = (List<Map<String, Object>>) user.get("Power");
Map<String, Object> usersFirstPower = userPowers.get(0);
Map<String, Object> firstPowerDamage = (Map<String, Object>) usersFirstPower.get("damage");
System.out.println(firstPowerDamage.get("first").toString());

Edit

This is another way to use Gson:

String yourJson = "...";
JsonArray map = (JsonArray) new JsonParser().parse(yourJson);
System.out.println(map.get(0).getAsJsonObject().get("Power").getAsJsonArray().get(0).getAsJsonObject().get("damage"));
Community
  • 1
  • 1
Sunchezz
  • 740
  • 6
  • 21
  • Oh wow the first method seems quite interesting, but one question.In your case it just grabs it for the first element called Power.there are like 40 Powers each having "damage" – Lol May 28 '19 at 12:16
  • therefore the variable `userPowers` is a list (like an array). You can iterate over it and do the steps after this for every element. – Sunchezz May 28 '19 at 12:19
  • And if the json file is located somewhere else, how do I call it? – Lol May 28 '19 at 12:51
  • Please ask a new question for this. But please do not ask for "somewhere else". This is a to general question. if you have a source for the json, ask how to read it with java. But please search for other already answered questions, because i am very sure the answer is still out there in StackOverflow. – Sunchezz May 28 '19 at 12:57