Question
Please help!! im trying to read this json file but for some reason keep getting errors Project1_input.json: { shelter_roster:[ { shelter_id:12513, animal_type:dog, animal_name:Lucky, animal_id:48934j, weight:
Please help!! im trying to read this json file but for some reason keep getting errors
Project1_input.json:
{ "shelter_roster":[ { "shelter_id":"12513", "animal_type":"dog", "animal_name":"Lucky", "animal_id":"48934j", "weight": 20, "receipt_date": 1515354694451 }, { "shelter_id":"15566", "animal_type":"cat", "animal_name":"Ms. Mouser", "animal_id":"1adf4", "weight":12, "receipt_date": 1515354694451 }, { "shelter_id":"15566", "animal_type":"bird", "animal_name":"Whistler", "animal_id":"1a545", "weight": 2, "receipt_date": 1515354694451 }, { "shelter_id":"336558", "animal_type":"rabbit", "animal_name":"Peter", "animal_id":"85545", "weight": 6, "receipt_date": 1515354694451 } ] }
My code:
package Main;
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;
import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException;
public class Main {
@SuppressWarnings("unchecked") public static void main(String[] args) { // JSON parser object to parse read file JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("Project1_input.json")) { // Read JSON file Object obj = jsonParser.parse(reader);
JSONArray animalList = (JSONArray) obj;
// JSONArray animalList = new JSONArray();
// Iterate over animal array animalList.forEach(animal -> parseUserObject((JSONObject) animal));
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } }
private static void parseUserObject(JSONObject animal) {
// Get user first name String shelter_id = (String) animal.get("shelter_id"); System.out.println(shelter_id);
// Get user first name String animal_type = (String) animal.get("animal_type"); System.out.println(animal_type);
String animal_name = (String) animal.get("animal_name"); System.out.println(animal_name);
// Get user last name String animal_id = (String) animal.get("animal_id"); System.out.println(animal_id);
// Get user website name Long weight = (Long) animal.get("weight"); System.out.println(weight);
// Get user email name Long receipt_date = (Long) animal.get("receipt_date"); System.out.println(receipt_date); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started