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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33638765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:56:08  来源:igfitidea点击:

How to Read JSON data from txt file in Java?

javajson

提问by Thomas Hu

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.

我现在想从本地 txt 文件(如下所示NODES.txt)读取一系列 JSON 数据(节点数据)。我使用 javax.json 来做到这一点。

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

目前,我有一个包含属性的Node 类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 !!

这是我需要检索的数据,它包含500多个节点,这里我只列出其中的3个,所以我需要使用循环来完成,我对此很陌生,请帮忙!!

The sample JSON data in NODES.txt

NODES.txt 中的示例 JSON 数据

[
 {
   "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
   }
  }
]

回答by Joel Min

First, read in the file and store the content in ONEString:

首先,读入文件并将内容存储在ONE 中String

BufferedReader reader = new BufferedReader(new FileReader("NODES.txt"));
String json = "";
try {
    StringBuilder sb = new StringBuilder();
    String line = reader.readLine();

    while (line != null) {
        sb.append(line);
        sb.append("\n");
        line = reader.readLine();
    }
    json = sb.toString();
} finally {
    reader.close();
}

Then parse the Json data from the String:

然后从 String 解析 Json 数据:

JSONObject object = new JSONObject(json); // this will get you the entire JSON node
JSONArray array = object.getJSONArray("Node"); // put in whatever your JSON data name here, this will get you an array of all the nodes

ArrayList<Node> nodeList = new ArrayList(array.length())<Node>;
for(int i=0; i<array.length(); i++){ // loop through the nodes
    JSONObject temp = array.getJSONObject(i);
    nodeList.get(i).setType(temp.getString("type")); //start setting the values for your node...
    ....
    ....
}

回答by Alain O'Dea

Create classes to represent the entries:

创建类来表示条目:

Feature.java:

功能.java:

import java.util.Map;

public class Node {
    public String type;
    public String id;
    public Geometry geometry;
    public Properties properties;
}

Geometry.java:

几何.java:

import java.util.List;

public class Geometry {
    public String type;
    public List<Double> coordinates;
}

Properties.java:

属性.java:

public class Properties {
     public String name;
}

And an application main class to drive the processing.

和一个应用程序主类来驱动处理。

Main.java:

主.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
        }
    }
}