2016-08-24 57 views
0

我試圖從本地計算機讀取json文件。如何解決Object類型的方法toString()不適用於參數(InputStream)

@Path("/") 
public class JsonParsing { 

    File f = new File("file.json"); 
    if (f.exists()){ 
     InputStream is = new FileInputStream("file.json"); 
     String jsonTxt = IOUtils.toString(is); 
     System.out.println(jsonTxt); 
     JSONObject json = new JSONObject(jsonTxt);  
     String a = json.getString("1000"); 
     System.out.println(a); 
    } 

} 

但我在toString()方法中出現錯誤。 也有可能從我的本地機器讀取包含json對象的.txt文件?如果可能的話,它是如何完成的?

+0

文件結尾無關緊要。只要打開你的'file.txt'而不是'file.json' –

+0

你正確導入'IOUtils'嗎? – Thilo

+0

你使用的是什麼版本的ioutils? –

回答

0
Firstly: whenever you ask the question- add imports as well. 
Secondly: yes it is possible to read a .txt file containing json object. 
Steps: 
1. Add Maven dependency in your pom.xml - 
<dependency> 
    <groupId>com.googlecode.json-simple</groupId> 
    <artifactId>json-simple</artifactId> 
    <version>1.1.1</version> 
</dependency> 

Or add the jar of this dependency. 

2.imports in java file - 

import java.util.Iterator; 
import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 

3. Sample Java code to read .txt file containing 
this json object 
{ 
    "Name": "crunchify.com", 
    "Author": "App Shah", 
    "Company List": [ 
     "Compnay: eBay", 
     "Compnay: Paypal", 
     "Compnay: Google" 
    ] 
} 

@SuppressWarnings("unchecked") 
    public static void main(String[] args) { 
     JSONParser parser = new JSONParser(); 

     try { 

      Object obj = parser.parse(new FileReader(
        "/Users/<username>/Documents/file1.txt")); 

      JSONObject jsonObject = (JSONObject) obj; 

      String name = (String) jsonObject.get("Name"); 
      String author = (String) jsonObject.get("Author"); 
      JSONArray companyList = (JSONArray) jsonObject.get("Company List"); 

      System.out.println("Name: " + name); 
      System.out.println("Author: " + author); 
      System.out.println("\nCompany List:"); 
      Iterator<String> iterator = companyList.iterator(); 
      while (iterator.hasNext()) { 
       System.out.println(iterator.next()); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

Hope it helps. 
0

嘗試:

File f = new File("file.json"); //it can be the same file "file.txt" 
    InputStream is = null; 
    if (f.exists()){ 
     try { 
      is = new FileInputStream("file.json"); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     BufferedReader r = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder stringBuilder = new StringBuilder(); 
     String line; 
     String jsString = null; 
     try { 
      while ((line = r.readLine()) != null) { 
       stringBuilder.append(line); 
      } 
      jsString = stringBuilder.toString(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     System.out.println(jsString); 
     JSONObject jsonObj = new JSONObject(jsString); 
     String a = jsonObj.getString("1000"); 
     System.out.println(a); 
    } 

參考: Creating JSONObject from text file

1

您可使用IOUtils和import sun.misc.IOUtils但默認不包括與參數(InputStream的)toString方法,所以你需要使用apache常用的io lib來使用這種方法和

import org.apache.commons.io.IOUtils; 
相關問題