java - 用java解析文件

标签 java json split

我需要解析这种格式的文件,以便我可以获取/获取代替这些标签的值。

我的文件如下所示:

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

最佳答案

你必须使用 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()));

关于java - 用java解析文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36146172/

相关文章:

javascript - 如何测试字符串是否为 JSON?

javascript - 我的 javascript 有困难

Python 子字符串 - 将第 n 个字符拆分到某个字符串的左侧

SQL Server 查询将花费的夜晚分成各自的 'Month"列

java - Android:WAITING对象出现

java - Windows 上的 NIO Files.createTempDirectory 导致异常

java - 从 Spring Boot 2.0.2 更新到 Spring Boot 2.1.2 导致 No Qualifying bean of type 异常

javascript - 将 JavaScript 函数编译成可调用的 Java 对象?

javascript - php多维数组到javascript无法通过循环访问

javascript - Node : Display POST data from another website