2

I need to parse a string that contains data of JSON file into a JSONObject and iterate over it's content to get it's keys and values for further treatement after. I'am stuck at parsing the content of a file after transforming it to a string. I tried to user parse() , quote() but it seems i'am missing a detail and i'am making a major mistake.

This is a snippet of the json file i treat :

{
    {
    "id":0,
    "name": "project1",
    "couverage": "100",
    "completness": "44.8",
    "consistency": "46",
    }
    
    {
    "id":1,
    "name": "project2",
    "couverage": "100",
    "completness": "44.8",
    "consistency": "46",
    }
    
    {
    "id":2,
    "name": "project3",
    "couverage": "100",
    "completness": "44.8",
    "consistency": "46",
    }
}

and this is the code i developed

public void readfromJsonFile(File jsonFile, long readTimeStamp) {
    logger.info("Read from JSON file:  {}", jsonFile.getName());
    
    try{
            
        //Read File Content
        String content = new String(Files.readAllBytes(jsonFile.toPath()));
        
        JSONParser jsonParser = new JSONParser(content);
        JSONObject obj = (JSONObject) jsonParser.parse();
        JSONArray key = obj.names();
        
        for (int i = 0; i < key.length (); ++i) {
            String keys = key.getString(i);
            System.out.println(keys);
            String value = obj.getString (keys);
            System.out.println(value);
    }catch (Exception e) {
            logger.error("Failed to parse JSON File: {}", jsonFile.getName());
        }
}

11
  • Your issue is while getting the key and the value? Commented Sep 16, 2020 at 15:12
  • Yes i cannot read my content string (or Json file) Commented Sep 16, 2020 at 15:17
  • Can I show one example with Jackson ? Would you appreciate that ? Commented Sep 16, 2020 at 15:17
  • 1
    @AnishB. Yes why not if it can help i would appreciate that Commented Sep 16, 2020 at 15:20
  • 3
    Is this json even correct, I don't see square brackets depicting it's an array or list. I don't see commas between each inner json section to separate elements. And even consistency have a comma in end, that too is wrong. This cannot even be called a json :) Commented Sep 16, 2020 at 15:34

2 Answers 2

1

You can use Jackson Databind as well.

Create a POJO class. For example :

public class POJO {

   private int id;
   private String name;
   private String couverage;
   private String completness;
   private String consistency;

   // getters, setters and constructors

}

Modify the JSON in the file.

[
    {
    "id":0,
    "name": "project1",
    "couverage": "100",
    "completness": "44.8",
    "consistency": "46"
    },
    {
    "id":1,
    "name": "project2",
    "couverage": "100",
    "completness": "44.8",
    "consistency": "46"
    },
    {
    "id":2,
    "name": "project3",
    "couverage": "100",
    "completness": "44.8",
    "consistency": "46"
    }
]

Code :

public void readfromJsonFile(File jsonFile, long readTimeStamp) {
    logger.info("Read from JSON file:  {}", jsonFile.getName());
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        POJO[] pojos = objectMapper.readValue(jsonFile, POJO[].class);
        for (int i = 0; i < pojos.length; ++i) {
            System.out.println(pojos[i].getId());
        }
    } catch (Exception e) {
            logger.error("Failed to parse JSON File: {}", jsonFile.getName());
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@NabilZouita Is this helpful ?
I have a model that will read those values (consistency, coverage, ...) can I use the model or I have to create a class with the attributes of the Json file ?
i spent some time testing but this was helpful thank you
0

Try it like this

JSONArray array_= new JSONArray(content);
JSONObject obj = (JSONObject) array_.get(0);// or any index

then you can use the Object.

1 Comment

still the same issue unfortunatly !

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.