0

I have some JSON which is subject to change but one constant is it will always contain multiple objects with two properties; text and answerText. An example JSON would be

{
    "food": {
        "eggTolerance": {
            "answerText": "None",
            "text": "What is your egg tolerance?"
        },
        "lactoseTolerance": null
    },
    "cookingExperience": {
        "experienceInLastFiveYears": {
            "answerText": "Yes",
            "text": "Was this experience within the last 5 years?"
        },
        "numberOfPies": {
            "answerText": "More than 50",
            "text": "How many pies have you baked?"
        },
        "significantPies": {
            "answerText": "More than 50",
            "text": "How many of these pies per quarter were at tasty?"
        },
        "spanOfExperience": {
            "answerText": "Yes",
            "text": "Do you have at least 12 months' experience baking pies?"
        }
    },
    "cocktails": {
        "manhattans": {
            "answerText": "The kiss of death",
            "text": "What have I done to deserve this flat, flavourless Manhattan?"
        },
        "Gin Martini": null
    },
    "waitressing": null
}

This can be changed by making it deeper or wider. For example the lactoseTolerance could have another object added to it or another object could be added to the root object.

How can I traverse this object to visit every object to get the properties of the deepest object?

I have seen this example but this just gives me the first level. In this instance I know I can then iterate the children of those objects but if the hierarchy changes the implementation is ruined.

Nanor
  • 2,400
  • 6
  • 35
  • 66
  • You can do the same thing as in the example link with recursion – libanbn May 27 '20 at 16:22
  • This is the first time I've written a recursive function since uni and the first production recursive function I've ever written. – Nanor May 27 '20 at 16:46

1 Answers1

0

I have used GSON lib: Please try below code:

pom.xml

      <dependency>
         <groupId>com.google.code.gson</groupId>
         <artifactId>gson</artifactId>
         <version>2.8.5</version>
       </dependency>

Then I have created QA class that have your fixed two properties; text and answerText

import com.google.gson.annotations.SerializedName;

public class QA {
    @SerializedName("answerText")
    private String answerText;
    @SerializedName("text")
    private String text;

    public QA(String answerText, String text) {
        this.answerText = answerText;
        this.text = text;
    }

    @Override
    public String toString() {
        return "QA{" +
                "answerText='" + answerText + '\'' +
                ", text='" + text + '\'' +
                '}';
    }


    public String getAnswerText() {
        return answerText;
    }

    public void setAnswerText(String answerText) {
        this.answerText = answerText;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


}

Now in driver code:

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        System.out.println("running!!");
        InputStream inputStream = Main.class.getResourceAsStream("json.json");
        String json = "{\n" +
                "  \"food\": {\n" +
                "    \"eggTolerance\": {\n" +
                "      \"answerText\": \"None\",\n" +
                "      \"text\": \"What is your egg tolerance?\"\n" +
                "    },\n" +
                "    \"lactoseTolerance\": null\n" +
                "  },\n" +
                "  \"cookingExperience\": {\n" +
                "    \"experienceInLastFiveYears\": {\n" +
                "      \"answerText\": \"Yes\",\n" +
                "      \"text\": \"Was this experience within the last 5 years?\"\n" +
                "    },\n" +
                "    \"numberOfPies\": {\n" +
                "      \"answerText\": \"More than 50\",\n" +
                "      \"text\": \"How many pies have you baked?\"\n" +
                "    },\n" +
                "    \"significantPies\": {\n" +
                "      \"answerText\": \"More than 50\",\n" +
                "      \"text\": \"How many of these pies per quarter were at tasty?\"\n" +
                "    },\n" +
                "    \"spanOfExperience\": {\n" +
                "      \"answerText\": \"Yes\",\n" +
                "      \"text\": \"Do you have at least 12 months' experience baking pies?\"\n" +
                "    }\n" +
                "  },\n" +
                "  \"cocktails\": {\n" +
                "    \"manhattans\": {\n" +
                "      \"answerText\": \"The kiss of death\",\n" +
                "      \"text\": \"What have I done to deserve this flat, flavourless Manhattan?\"\n" +
                "    },\n" +
                "    \"Gin Martini\": null\n" +
                "  },\n" +
                "  \"waitressing\": null\n" +
                "}";

                final Gson gson = new Gson();


                Type type = new TypeToken<Map<String, Map<String, QA>>>(){}.getType();
                Map<String, Map<String, QA>> myMap = gson.fromJson(json, type);
                System.out.println("Data:"+ myMap.get("food").get("eggTolerance").getAnswerText());


    }


}