2016-03-22 104 views
1

我需要解析這種格式的文件,以便我可以獲取/獲取存在於這些標記位置的值。使用java解析文件

我的文件是這樣的:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":701,"main":"Mist","description":"mist","icon":"50d"},{"id":721,"main":"Haze","description":"haze","icon":"50d"}],"base":"stations","main":{"temp":277.93,"pressure":1031,"humidity":81,"temp_min":275.15,"temp_max":281.15},"visibility":8000,"wind":{"speed":3.6,"deg":50},"clouds":{"all":0},"dt":1458206447,"sys":{"type":1,"id":5091,"message":0.0163,"country":"GB","sunrise":1458194902,"sunset":1458238189},"id":2643743,"name":"London","cod":200} 

我需要獲取的每一個值,需要消除所有的分隔符。

例如,我需要每個變量的新屬性。

喜歡:

coord:- lon:0.13, lat:51.51 
weather:-id:701, 
main:Mist, 
description:mist 
icon:50d 

我已經添加上我工作的代碼。我嘗試使用拆分操作進行解析,但它不起作用。我也嘗試過使用JSONParser和掃描儀操作。 代碼如下:

package com.weather; 
import java.util.Scanner; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.io.*; 
import java.net.*; 
import java.util.StringTokenizer; 
import org.json.*; 
import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 


public class webApp 
{ 
    public String getwebApp(String url) 
    { 
     try 
     { 
       URL WeatherDisp=new URL(url); 
       URLConnection yc = WeatherDisp.openConnection(); 
       BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); 
       String inputLine; 
       inputLine=in.readLine(); 
       return inputLine; 
     } 
     catch(Exception e) 
     { 
      return e.getMessage(); 
     } 
    } 


public static void main(String [] url) 
{ 

    //private static final 
    String filePath = "C:\\Users\\abc\\Downloads\\weather\\temp.json"; 
    Path path = Paths.get(filePath); 

    try 
    { 
     webApp obj=new webApp(); 
     String a; 
     a=obj.getwebApp("http://api.openweathermap.org/data/2.5/weather?      q=London,uk&appid=6fc5e84445c330ed737da5dee07d1866"); 
     System.out.println(a); 

     File file=new File("temp.json"); 
     file.createNewFile(); 
     if(!file.exists()) 
      file.createNewFile(); 

     BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile())); 
     bw.write(a); 
     bw.close(); 

     Scanner scanner = new Scanner(path); 

      //read file line by line 
      scanner.useDelimiter(System.getProperty("line.separator")); 
      while(scanner.hasNext()) 
      { 
       System.out.println("Lines: "+scanner.next()); 
      } 
      scanner.close(); */ 

     FileReader input = new FileReader("temp.json"); 
     BufferedReader bufRead = new BufferedReader(input); 
     String myLine = null; 

     while ((myLine = bufRead.readLine()) != null) 
     {  
      String[] array1 = myLine.split("}"); 
      System.out.println(array1); 
     } 
     bufRead.close(); 



    /* FileReader reader = new FileReader(filePath); 

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

     String coord=(String)jsonObject.get("coord"); 
     System.out.println(coord); */ 

     } 

      catch(Exception e1) 
      { 
       System.out.println("Error during reading/writing"); 
      } 
} 
} 
+0

這是JSON;使用JSON解析器。嘗試谷歌的Gson傳遞一種'Map.class' – Bohemian

回答

0

您必須使用JSONObject。下面是一個例子

String someText = " {\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":" 
     + "[{\"id\":701,\"main\":\"Mist\",\"description\":\"mist\",\"icon" 
     + "\":\"50d\"},{\"id\":721,\"main\":\"Haze\",\"description\":" 
     + "\"haze\",\"icon\":\"50d\"}],\"base\":\"stations\",\"main\":" 
     + "{\"temp\":277.93,\"pressure\":1031,\"humidity\":81,\"temp_min\":275.15,\"temp_max\":281.15},\"visibility\":8000,\"wind\":{\"speed\":3.6,\"deg\":50},\"clouds\":{\"all\":0},\"dt\":1458206447,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.0163,\"country\":\"GB\",\"sunrise\":1458194902,\"sunset\":1458238189},\"id\":2643743,\"name\":\"London\",\"cod\":200}"; 
JSONObject obj = new JSONObject(someText); 
System.out.println("Coord " + obj.get("coord")); 

讀取文件,你可以使用

JSONObject obj = new JSONObject(Files.readAllBytes(new File("YourFile").toPath())); 
+0

請提及使用哪個JSON庫,因爲'JSONObject'不是JDK的一部分 –

+0

來自www.json.org的JSON庫 –