java如何读取json数据,如何从Java中的txt文件读取JSON数据?

I am now want to read a series of JSON data(Nodes data) from local txt file(shown below, NODES.txt ). I use javax.json to do this.

Currently, I have a Node class which contains the attributes:type, id, geometry class(contains type, coordinates), properties class (contains name);

Here is the data I need to retrieve, it contains more than 500 nodes in it, here I just list 3 of them, so I need to use a loop to do it, I am quite new to this, please help !!

The sample JSON data in NODES.txt

[

{

"type" : "Feature",

"id" : 8005583,

"geometry" : {

"type" : "Point",

"coordinates" : [

-123.2288,

48.7578

]

},

"properties" : {

"name" : 1

}

},

{

"type" : "Feature",

"id" : 8005612,

"geometry" : {

"type" : "Point",

"coordinates" : [

-123.2271,

48.7471

]

},

"properties" : {

"name" : 2

}

},

{

"type" : "Feature",

"id" : 8004171,

"geometry" : {

"type" : "Point",

"coordinates" : [

-123.266,

48.7563

]

},

"properties" : {

"name" : 3

}

},

****A Lot In the Between****

{

"type" : "Feature",

"id" : 8004172,

"geometry" : {

"type" : "Point",

"coordinates" : [

-113.226,

45.7563

]

},

"properties" : {

"name" : 526

}

}

]

解决方案

Create classes to represent the entries:

Feature.java:

import java.util.Map;

public class Node {

public String type;

public String id;

public Geometry geometry;

public Properties properties;

}

Geometry.java:

import java.util.List;

public class Geometry {

public String type;

public List coordinates;

}

Properties.java:

public class Properties {

public String name;

}

And an application main class to drive the processing.

Main.java:

import com.google.gson.Gson;

import java.io.FileReader;

import java.io.Reader;

public class Main {

public static void main(String[] args) throws Exception {

try (Reader reader = new FileReader("NODES.txt")) {

Gson gson = new Gson();

Node[] features = gson.fromJson(reader, Node[].class);

// work with features

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,可以使用各种方式读取txt文件并将其内容转换为JSON数据。以下是一种常见的方法: 1. 首先,你需要导入相关的库,包括JSON库和文件读取库。可以使用JSON库如Gson或Jackson来处理JSON数据,使用Java的IO库来读取文件。 2. 创建一个用于读取文件的File对象,并传入txt文件的路径作为参数。 3. 使用BufferedReader类来读取文件内容。可以使用readLine()方法逐行读取文件内容,并将每行的数据存储在一个字符串变量。 4. 将读取到的字符串数据转换为JSON格式。可以使用JSON库提供的方法将字符串转换为JSON对象或JSON数组。 5. 最后,将JSON对象或数组作为结果返回。 下面是一个示例代码: ```java import java.io.BufferedReader; import java.io.FileReader; import com.google.gson.Gson; public class TxtToJsonConverter { public static void main(String[] args) { String filePath = "path/to/your/txt/file.txt"; String jsonData = convertTxtToJson(filePath); System.out.println(jsonData); } public static String convertTxtToJson(String filePath) { StringBuilder sb = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(filePath)); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); } catch (Exception e) { e.printStackTrace(); } // 使用Gson库将字符串转换为JSON格式 Gson gson = new Gson(); return gson.toJson(sb.toString()); } } ``` 请注意,上述代码仅提供了一个基本的示例,实际应用可能需要根据具体的需求进行适当的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值