-2

I thanks for taking a look.

I'm working on a problem where I need to be able to get a Json into memory and be able to "navigate" around the keys.

I am able to use the Json.createParser() method and use the FileReader in the args to get the file in my system. My problem is that from there I don't yet have a way to take the JsonParser object to JsonObject to read values from.

I am using the package javax.json.

I want to be able to navigate around the Json data I have easily with JsonObject and JsonArray but using the JsonParser object.

Thanks for your time.

JoeS
  • 25
  • 2
  • What do you mean by "navigate" around the keys? JsonParser is pretty low-level. It lets you interpret JSON as a stream of typed tokens. You might be more interested in JsonReader if you just want to read the entire tree into memory. – David Ehrmann May 30 '17 at 03:31
  • I think I mean I generally want to go deeper into the objects of the objects of the arrays of the objects lol. I'm going to look at JsonReader thank you. – JoeS May 30 '17 at 04:12

1 Answers1

1

Okay I figured it out.

Here is what the json looks like:

{
    "name": "Changed name",
    "Items": 
        [ 
            {"item1": "the_fist_item"},
            {"item2": "the_second_item"},
            {"item3": "the_second_item"}
        ]
}

then inside the java...

import java.io.FileReader;
import javax.json.*;

public class JsonTester
{
  public static void main(String[] args)
  { 
    try
    {

      JsonReader json_file = Json.createReader(new FileReader("***the directory to the json file***"));
      JsonObject json_object = json_file.readObject();

      String the_second_item = json_object.getJsonArray("Items").getJsonObject(1).get("item2").toString();

      System.out.println(the_second_item);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

The navigating part I was trying to accomplish is on the_second_item. Which is all the get() methods to go deeper into the Json file to get the specific value I wanted.

Thanks to David Ehrmann for his help.

JoeS
  • 25
  • 2