user6149776
user6149776

Reputation:

Trouble parsing long in JSON

I posted here earlier and was flagged for my post being too similar to another previously answered. I don't believe this is, so have tried to reword it.

I have a JSON file where value is either a long, or can contain an object and a array:

{
"declaration-list" : [


    {
        "declared-variable" : "x301",
        "value" : {
            "operator" : "set",
            "arguments" : [
                0,
                1
              ]
          }
      },

    {
        "declared-variable" : "x112",
        "value" : -1
      },

  ]
}

I have written the following code in java to parse the file and have managed to extract declared-variable from the array (Note: I'm using org.json.simple):

public static void getInput() throws IOException, ParseException {

    JSONParser parser = new JSONParser();

    File file = new File("myfile");

    Object object = parser.parse(new FileReader(file));

    JSONObject jsonObject = (JSONObject) object;

    JSONArray jasonArray = (JSONArray) jsonObject.get("declaration-list");

    for (Object JSI : jasonArray) {

        if (JSI instanceof JSONObject) {

            JSONObject declared_variable = (JSONObject) JSI;
            String decVar = (String) declared_variable.get("declared-variable");
            System.out.println(decVar);

            JSONObject value = (JSONObject) JSI;

            String operator = (String) value.get("operator");
            System.out.println(operator);
        }

    }

}

How do I get a long from a JSONArray and deal with the case where value contains another JSONObject and JSONArray?

I hope this post is more clear, thanks.

Upvotes: 1

Views: 69

Answers (1)

It seems you are using the dependency (the version may differ):

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

The appropriate value type interpretation (i.e. handling the value corresponding to its type) should be implemented.

Consider the following draft example. Take a look at the implementation of the handleDeclaredVariable method to get the basic understanding: it should answer your questions.

Update the loop:

for (final Object JSI : jasonArray) {
    if (JSI instanceof JSONObject) {
        handleDeclaredVariable((JSONObject) JSI);
    }
}

Introduce the method:

private static void handleDeclaredVariable(final JSONObject variable) {
    final String variableName = (String) variable.get("declared-variable");
    final Object value = variable.get("value");
    if (value instanceof JSONObject) {
        final JSONObject jsonValue = (JSONObject) value;
        final String operator = (String) jsonValue.get("operator");
        final JSONArray arguments = (JSONArray) jsonValue.get("arguments");
        System.out.println(
            String.format(
                "The value of the variable %s is a JSON object: operator: %s, arguments: %s",
                variableName,
                operator,
                arguments
            )
        );
    } else if (value instanceof Number) {
        final Number numberValue = (Number) value;
        System.out.println(
            String.format(
                "The value of the variable %s is a Number: %s",
                variableName,
                numberValue
            )
        );
    } else {
        System.err.println(
            String.format(
                "The value of the variable %s has unsupported type (%s): %s",
                variableName,
                value.getClass().getCanonicalName(),
                value
            )
        );
    }
}

Upvotes: 1

Related Questions