我需要解析这种格式的文件,以便我可以获取/获取代替这些标记的值。
我的文件如下:
{"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 :(得分: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()));