0

I have a file query.json that contains [["Rain"], ["Cloudy", "Sprinkler"], [false, true]] to represent a query for a Bayesian network.

String queryContents = readEntireFile(new File("query.json"));
Query query = Query.queryFromString(queryContents);

Is used to read the entire file and then call the method to create the query.

When I call my queryFromString(String s) method I get the error:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING

my queryFromString method is

    public static Query queryFromString(String s) { 
    Gson gson = new Gson();  
    JsonParser parser = new JsonParser();
    JsonArray jsonNodes = parser.parse(s).getAsJsonArray();
    JsonElement element = jsonNodes.get(0);
    JsonArray jsonNode = element.getAsJsonArray();
    String [] q = gson.fromJson(jsonNode.get(0), String[].class);
    String [] e = gson.fromJson(jsonNode.get(1), String[].class);
    boolean[] v = gson.fromJson(jsonNode.get(2), boolean[].class);



    return null;
    }

This is my first time using JSON, so I' not really sure why it is producing this error, any help?

1 Answer 1

1

Your root JSON value is a JSON array containing 3 other JSON values: two JSON arrays containing string values and a JSON array containing boolean values.

You should be operating on the root JSON array directly.

JsonArray jsonNodes = parser.parse(s).getAsJsonArray();
String[] q = gson.fromJson(jsonNodes.get(0), String[].class);
String[] e = gson.fromJson(jsonNodes.get(1), String[].class);
boolean[] v = gson.fromJson(jsonNodes.get(2), boolean[].class);
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh okay, thank you...Its the little things that get you sometimes.

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.