用java解析文件
我需要解析这种格式的文件,这样我就可以获得/获取那些标记所在的值 我的文件如下所示:用java解析文件,java,json,split,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
{"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和scanner操作。
代码如下:
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()));
您必须使用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()));
它是JSON;使用JSON解析器。尝试谷歌的Gson传递一种类型的
Map.class
JSON;使用JSON解析器。尝试google的Gson传递一种类型的Map.class
请说明使用了哪个JSON库,因为JSONObject
不是www.JSON.org中的JDKJSON库的一部分请说明使用了哪个JSON库,因为JSONObject
不是www.JSON.org中的JDKJSON库的一部分