2016-06-14 98 views
4

所以我在閱讀Java中的Json文件時遇到了麻煩。閱讀Java中的json文件

它與內容的JSON文件格式如下:

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

這是我使用的代碼:

package controllers; 


@Named(value = "cityID") 
@SessionScoped 
public class getCityIDs implements Serializable { 



    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"; 
    } 
} 

但是,它給了我一個錯誤在這行:

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

是:

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) 

由於某些原因,它無法讀取文件路徑? 「未知來源」? 我不知道我在做什麼錯。

當我調用另一個類中的方法時,該方法返回一個「0」,並且國家名稱爲「Neustadt」。 基本上所有我想要的是這個函數返回某個城市的ID。 名稱與ID一起存儲在Json中。

編輯: 理想情況下,我想能夠解析JSON文件,它位於項目內部。我試過使用.getClass()。getResource(「/ path/to/json」);但這根本不起作用。

編輯:固定

package controllers; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
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; 

@Named(value = "cityID") 
@SessionScoped 
public class getCityIDs implements Serializable{ 



    JSONObject jsonObject; 
    public long getCityIDs(String name) { 

     try { 

      JSONParser parser = new JSONParser(); 

      InputStream in = getClass().getResourceAsStream("/dataSteden/stedenNamen1.json"); 

      try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) { 
      String line; 
      while ((line = br.readLine()) != null) { 
       jsonObject = (JSONObject) parser.parse(line); 
       } 
      } 

      String travelName = (String) jsonObject.get("name"); 
      System.out.println("stad: " +travelName); 
      System.out.println("testttt"); 
      if(travelName.equals(name)){ 
       long id = (long) jsonObject.get("_id"); 
       System.out.println(id); 
       return id; 
      } else { 
       System.out.println("else"); 
       return 5; 
      } 

     } 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"; 
    } 
} 
+0

未知的來源意味着您確實擁有simpleJSON庫的源代碼。另外,嘗試堅持使用Java變量命名約定。你的類不應該被稱爲'getCityIds'。該方法可以,但。 –

+0

我不確定你的意思是第一行 – aze45sq6d

+0

不用擔心。 Java stacktrace試圖爲您提供您正在使用的JSON庫的行號和文件名,但它不能。這就是輸出中「未知來源」的含義。關於實際的錯誤,你已經顯示了有效的JSON,所以我不明白你爲什麼得到這個錯誤。 –

回答

5

你的數據線分隔

{"_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}} 

因此,你不能把整個文件轉換成JSONParser,你必須讀取文件中的行逐並將每行解析爲JSONObject,從中可以提取出所需的鍵值。

+0

萬分感謝! 現在我想知道,該文件位於我的項目。 我不想在本地計算機上調用它,因爲有更多的人在這個項目上工作。但我似乎無法做到。理想情況下,我想要這樣的東西:FileReader reader = new FileReader(this.getClass()。getResource(「/ json/city.list.json」)); – aze45sq6d

+0

對。只要你有該文件位於該項目的src /文件夾根目錄下的某個json文件夾中,我認爲你在那裏應該可以工作 –

+0

我把它放在Source Packages下,並且這樣稱呼它: FileReader reader =新的FileReader(this.getClass())。。的getResource( 「/ JSON/city.list」)的toString()); 但是它給了我一個nullpointerexception,它現在在本地調用它的時候就起作用,一行一行地調用它 – aze45sq6d