3

I'm new in java and i have this maybe simple faculty project. I must to parse json with eclipse, so i started but without any success. I don't know how to start when i have a multiple object in json.

I started like this:

public static void main(String[] args) {

         FileReader reader = new FileReader(filePath);


            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
            JSONArray objectArray = jsonObject.getJSONArray("product");


            //JSONObject site= jsonSites.getJSONObject(1);

            long elementaryProductId =  (long) jsonObject[0].get("elementaryProductId");
            System.out.println("The id is: " + elementaryProductId);'

And thi is my json file:

[{  
    "elementaryProductId":1,
    "bonusMalus":30,
    "deductible":500,
    "comprehensive":1,
    "partial":0,
    "legacyPremium":130,
    "product":{  
        "productId":2,
        "garage":"true",
        "constructionYear":1990,
        "region":"East",
        "dateOfBirthYoungest":"1983-06-22",
        "objectValue":25000,
        "type":"Car",
        "insuredObject":{  
            "name":"Car",
            "ownersName":"Jovana",
            "mileage":300000,
            "engineCapacity":120
        },
        "salesProduct":{  
            "salesProductId":3,
            "currency":"EUR",
            "contractStart":"2011-01-01",
            "contractEnd":"2012-01-01"
        },
        "productType":"Car"
    }
},
{  
    "elementaryProductId":1,
    "bonusMalus":5,
    "deductible":100,
    "comprehensive":1,
    "partial":0,
    "legacyPremium":75.38,
    "product":{  
        "productId":2,
        "garage":"true",
        "constructionYear":2005,
        "region":"East",
        "dateOfBirthYoungest":"1999-06-22",
        "objectValue":30000,
        "type":"Car",
        "insuredObject":{  
            "name":"Car",
            "ownersName":"Jelena",
            "mileage":300000,
            "engineCapacity":210
        },
        "salesProduct":{  
            "salesProductId":3,
            "currency":"EUR",
            "contractStart":"2013-01-01",
            "contractEnd":"2014-01-01"
        },
        "productType":"Car"
    }
}]
8
  • 2
    Welcome to Stackoverflow, Pepinho. Do you get any error? What are your results compared to your expectations? Please add this to your question by editing. Commented Dec 25, 2014 at 13:19
  • What library are you using for JSON parsing? Commented Dec 25, 2014 at 13:19
  • Be sure to add the complete code, the full JSON input and the stacktrace of any exception you may have encountered (or the incorrect results vs. the expected ones). Commented Dec 25, 2014 at 13:20
  • I'm using json-simple-1.1.1.jar. Commented Dec 25, 2014 at 13:23
  • Before that i have errors :JSONArray objectArray = jsonObject.getJSONArray(""); Commented Dec 25, 2014 at 13:25

1 Answer 1

2

I got it to work with the following:

public static void main(String[] args) throws IOException, ParseException{
    FileReader reader = new FileReader(new File("filename.json"));


    JSONParser jsonParser = new JSONParser();
    JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
    JSONObject object = (JSONObject) jsonArray.get(0);
    long elementaryProductId = (Long) object.get("elementaryProductId");



    System.out.println("The id is: " + elementaryProductId);
}

Explanation of the above:

You know the outermost element is an array so parse straight into a JSONArray. Next you want to pull out the first element of that array which is a JSONObject (its in braces). After that the code should be fairly self explanatory :)

Sign up to request clarification or add additional context in comments.

1 Comment

Have you imported java.io.File;? On that class in eclipse just press ctrl+shift+o (organize imports) and it should sort that out

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.