aze45sq6d aze45sq6d - 5 months ago 146
JSON Question

Reading a json file in Java

So I am having troubles with reading a Json file in Java.

It is a Json file with content in this format:

{"_id":2864071,"name":"Neustadt","country":"DE","coord":{"lon":12.56667,"lat":52.400002}}


This is the code I am using:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controllers;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
*
* @author Jean
*/
@Named(value = "cityID")
@SessionScoped
public class getCityIDs implements Serializable{

//Parser
private static final String filePath = "C:\\Users\\User\\Documents\\NetBeansProjects\\PhpProject1\\Reis\\RBureau222\\web\\json\\city.list.json";

public long getCityIDs(String name) {

//Read the json file


try {

FileReader reader = new FileReader(filePath);

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(reader);

// get a number from the JSON object

String travelName = (String) jsonObject.get("name");

if(travelName.equals(name)){
long id = (long) jsonObject.get("_id");
System.out.println(id);
return id;
}else{
System.out.println("else");
return 0;
}

} catch (FileNotFoundException ex) {
Logger.getLogger(getCityIDs.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | ParseException ex) {
Logger.getLogger(getCityIDs.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("einde functie");
return 0;
// JSONObject jsonObject = (JSONObject) parser.parse(getClass().getResource("/json/city.list.json").toString());
}

public String test(){
return "hello world";
}
}


However, it gives me an error at this line:

JSONObject jsonObject = (JSONObject) parser.parse(reader);


being:

Severe: Unexpected token LEFT BRACE({) at position 88.
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at controllers.getCityIDs.getCityIDs(getCityIDs.java:45)


For some reason it can't read the filepath? "Unknown source"?
I'm not sure what I'm doing wrong.

The method just returns a "0" when I call the method in another class, with as country name "Neustadt".
Basically all I want is for this function to return the ID for a certain city.
The names are stored in the Json, together with the ID.

Edit:
Ideally I want to be able to parse the JSON file, which is located inside the project.
I tried using .getClass().getResource("/path/to/json"); but that didn't work at all.

Answer

Your data is line-delimited

{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}

Therefore, you cannot throw the entire file into a JSONParser, you must read the file line-by-line and parse each line as a JSONObject, from which you can extract out the needed key-values.

Comments