1

Im a begginer programmer, and im trying to make my first own, small project, that "extracts" values from JSON file, and then later through that i draw it in on javafx line chart. I was trying to find a solution for my problem of extracting values from JSON file (using json.simple or/and json.org) but i still cant manage to do it properly.

The inside of my json file is:

[
  {
    "Price": 7999,
    "Id": 0,
    "Datetime": "2020-06-09T21:55:55.335Z"
  },
  {
    "Price": 7500,
    "Id": 1,
    "Datetime": "2020-06-10T21:55:55.335Z"
  },
  {
    "Price": 7900,
    "Id": 2,
    "Datetime": "2020-06-11T21:55:55.335Z"
  },
  {
    "Price": 6800,
    "Id": 3,
    "Datetime": "2020-06-12T21:55:55.335Z"
  },
  {
    "Price": 6949,
    "Id": 4,
    "Datetime": "2020-06-13T21:55:55.335Z"
  },
  {
    "Price": 9160,
    "Id": 5,
    "Datetime": "2020-06-14T21:55:55.335Z"
  },
  {
    "Price": 7500,
    "Id": 6,
    "Datetime": "2020-06-15T21:55:55.335Z"
  },
  {
    "Price": 6999,
    "Id": 7,
    "Datetime": "2020-06-16T21:55:55.335Z"
  },
  {
    "Price": 7259,
    "Id": 8,
    "Datetime": "2020-06-17T21:55:55.335Z"
  },
  {
    "Price": 7259,
    "Id": 9,
    "Datetime": "2020-06-18T21:55:55.335Z"
  },
  {
    "Price": 6700,
    "Id": 10,
    "Datetime": "2020-06-19T21:55:55.335Z"
  },
  {
    "Price": 5999,
    "Id": 11,
    "Datetime": "2020-06-20T21:55:55.335Z"
  },
  {
    "Price": 5500,
    "Id": 12,
    "Datetime": "2020-06-21T21:55:55.335Z"
  },
  {
    "Price": 6200,
    "Id": 13,
    "Datetime": "2020-06-22T21:55:55.335Z"
  },
  {
    "Price": 6260,
    "Id": 14,
    "Datetime": "2020-06-23T21:55:55.335Z"
  },
  {
    "Price": 5800,
    "Id": 15,
    "Datetime": "2020-06-24T21:55:55.335Z"
  },
  {
    "Price": 5300,
    "Id": 16,
    "Datetime": "2020-06-25T21:55:55.335Z"
  },
  {
    "Price": 5090,
    "Id": 17,
    "Datetime": "2020-06-26T21:55:55.335Z"
  },
  {
    "Price": 4999,
    "Id": 18,
    "Datetime": "2020-06-27T21:55:55.335Z"
  },
  {
    "Price": 4999,
    "Id": 19,
    "Datetime": "2020-06-28T21:55:55.335Z"
  }
]

And im interested in extracting the "Price" and "Datetime" values from it. I was trying to use JSON Array and iterate through it, but iterator couldnt work (and i have no idea why). I was able to get the values into a graph using filereader and buffered reader (from simple .txt file filled with only raw price) but not from JSON. So how exactly i can do it?

This is my first post on stackoverflow and as i said im a beginner programmer, im sorry if my question is stupid, but i just cant do it even though i've been searching for solution for few hours now and none of them worked for me.

Thanks

Insekure
  • 89
  • 1
  • 5

1 Answers1

0

There are many opensource libraries available which can help you out.

But since you mentioned using JsonSimple ('com.googlecode.json-simple:json-simple:1.1.1') here are two approaches with it:

try (FileReader reader = new FileReader(ClassLoader.getSystemResource(filePath).getFile())) {
            // read the json file
 
 
            JSONParser jsonParser = new JSONParser();
            JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
 
            Iterator i = jsonArray.iterator();
 
            // take each value from the json array separately
            while (i.hasNext()) {
                JSONObject innerObj = (JSONObject) i.next();
                System.out.println("price " + innerObj.get("Price") +
                        " with id " + innerObj.get("Id"));
            }
}

For Loop approach: (Since with iterator you are facing issues)

try (FileReader reader = new FileReader(ClassLoader.getSystemResource(filePath).getFile())) {
      JSONParser jsonParser = new JSONParser();
      JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
      for(Object o: jsonArray){
        if ( o instanceof JSONObject) {
          JSONObject jsonObject = (JSONObject) o;
          System.out.println("price " + jsonObject.get("Price") +
              " with id " + jsonObject.get("Id"));
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
  • There is a slight problem, i cant (for some reason) use iterator, as it says that it cant resolve the method. – Insekure Jun 30 '20 at 17:24
  • @Insekure I have added the for loop way too.. please give a try. – Abhinaba Chakraborty Jun 30 '20 at 18:25
  • Ok, so i tried it - and it kinda worked, but after i changed the JSONArray's type to JSON.simple (for some reason i wouldnt delete my org.Json lib, as earlier it just showed that foreach is not applicable to org.jsonArray. I got nullpointer exception though in try(FileReader . . . . . ) Perhaps the org. lib messes up the code? Anyway thank you so much for the answers, but i still need to figure out what is messing the code up... – Insekure Jun 30 '20 at 19:09
  • import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; These are the imports you need. – Abhinaba Chakraborty Jun 30 '20 at 19:15
  • Yep, still getting a nullpointer exception at try (FileReader reader = new FileReader(ClassLoader.getSystemResource("src\\data.json").getFile())). How can i upload the code? Perhaps there is something wrong in class itself. Im working on javafx template with Controller, and im doing it in a separate class. But it shouldnt mess up with anything – Insekure Jun 30 '20 at 19:26
  • You need to keep the file in src/main/resources folder (which is in classpath) . and do FileReader reader = new FileReader(ClassLoader.getSystemResource("data.json").getFile()) – Abhinaba Chakraborty Jun 30 '20 at 19:27
  • i've put it my project folder in src so i can have easy access to it, maybe i should try to give a path to this file on desktop or something? – Insekure Jun 30 '20 at 19:31
  • Dont put directly under src. Since you are using classloader, it will append classpath which is src/main/resources – Abhinaba Chakraborty Jun 30 '20 at 19:33
  • Linked the path to the file in a desktop, but still getting nullpointer exception. Hm – Insekure Jun 30 '20 at 19:36
  • No no. Dont give absolute path with classloader. – Abhinaba Chakraborty Jun 30 '20 at 19:38
  • Im sorry, as you can clearly see im not very advanced it and i dont really get how my filepath should look in this case. Thank you for your patience :) – Insekure Jun 30 '20 at 19:41
  • Put the file in src/main/resources directory and use this code: FileReader reader = new FileReader(ClassLoader.getSystemResource("data.json").getFile()) – Abhinaba Chakraborty Jun 30 '20 at 19:41
  • How can i create src/main/resources dir in intellij? – Insekure Jun 30 '20 at 19:54
  • While creating a new java project it automatically gets created bro. – Abhinaba Chakraborty Jun 30 '20 at 20:07
  • Unfortunately I dont have it ;-; just src but not main/resources dir – Insekure Jun 30 '20 at 20:24
  • https://stackoverflow.com/questions/18717038/adding-resources-in-intellij-for-maven-project Check this – Abhinaba Chakraborty Jun 30 '20 at 20:28
  • Thank you so much. Today i retinkered few things and using your code i managed to do it. To anyone wondering, i eliminated the ClassLoader.getSystemResource and getFile() method, giving a basic path to a file placed in my src dir, and it worked. Thank you so much for your patience and for this code, now it works and i know how to read JSON files now, it is a new lesson and knowledge for me. Once again, thank you and have a good day :) – Insekure Jul 01 '20 at 15:10