【问题标题】:How to parse/read specific data from .lua file?如何从 .lua 文件中解析/读取特定数据?
【发布时间】:2020-07-13 05:38:46
【问题描述】:

我正在尝试从 file.lua 中读取数据。
file.lua 包含:

Settings = {
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [1]
    {
        ["msg"] = "цлк25н ппал шп маг ршам рдру сова (осколки анрол) 5 слотов",
        ["aut"] = "Savagemode",
        ["cha"] = "5. Поиск спутников",
    }, -- [2]
    {
        ["msg"] = "В СА25 танк/хил/дд/рдд",
        ["aut"] = "Dralo",
        ["cha"] = "5. Поиск спутников",
    }, -- [3]
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [4]
    {
        ["msg"] = "продам |cffa335ee|Hitem:36919:0:0:0:0:0:0:0:80|h[Багровый рубин]|h|rх6 по 120г",
        ["aut"] = "Аматин",
        ["cha"] = "2. Торговля: Город",
    }, -- [5]
}

我如何能够用 java 代码读取这个文件? Settings 是完整的消息日志。 msg - 消息,aut - 消息作者,cha - 消息频道
这就是我要使用的:我的pom.xml、java 代码和运行结果

<dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
</dependency>

JSONParser jsonParser = new JSONParser();

try (FileReader reader = new FileReader(filePath))
{
    //Read JSON file
    Object obj = jsonParser.parse(reader);

    JSONArray employeeList = (JSONArray) obj;
    System.out.println(employeeList);

    //Iterate over employee array
    //employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );

} 

//Run result:

Unexpected character (S) at position 2.
    at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
    at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
    at Main.main(Main.java:21)


我想得到任何建议,我应该使用什么。因为在我读完这个文件后,我想通过调用getMsg() 方法来检查变量 msg 中的内容;
我无法在一个字符串中读取此内容,但我如何处理数据?

File myObj = new File(filePath);
      Scanner myReader = new Scanner(myObj);
      while (myReader.hasNextLine()) {
      String data = myReader.nextLine();
      System.out.println(data);
}
myReader.close();

//result
Settings = {
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [1]
    {
        ["msg"] = "цлк25н ппал шп маг ршам рдру сова (осколки анрол) 5 слотов",
        ["aut"] = "Savagemode",
        ["cha"] = "5. Поиск спутников",
    }, -- [2]
    {
        ["msg"] = "В СА25 танк/хил/дд/рдд",
        ["aut"] = "Dralo",
        ["cha"] = "5. Поиск спутников",
    }, -- [3]
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [4]
    {
        ["msg"] = "продам |cffa335ee|Hitem:36919:0:0:0:0:0:0:0:80|h[Багровый рубин]|h|rх6 по 120г",
        ["aut"] = "Аматин",
        ["cha"] = "2. Торговля: Город",
    }, -- [5]
}

Process finished with exit code 0


【问题讨论】:

  • 为什么要使用 JSON 解析器读取 lua 文件?它们不一样。
  • 你的文件是一个lua脚本文件。要使用 Gson,您的文件必须遵循 JSON 约定。这不是
  • 我试图改进这个问题的标题,因为你不是在问如何读取任何文件,而是如何从 lua 文件中读取数据。
  • 有大量关于如何在 Java 中读取文件的教程。他们有什么问题?

标签: java lua


【解决方案1】:

Lua 是一种编程语言,而不是数据语言,因此文件不应解析,而应执行。 p>

您可以编写一个 Lua 脚本来执行此脚本并将其编码为 JSON。

  • 首先使用 Lua 解释器安装 luarocks(Windows 的 luarocks 与该解释器捆绑在一起,如果在 linux 上,请使用包管理器安装 lualuarocks)。
  • 安装 json-lua 库以将数据编码为 JSON:
luarocks install json-lua
  • script.lua 中编写以下 Lua 脚本:
local JSON = require("JSON") --The JSON library

local source = "file.lua"
local destination = "file.json"

dofile(source)

--Now after executing the file.lua script, the data has been stored in the global table Settings.

--Encode into JSON data.
local jsonData = JSON:encode_pretty(Settings)

--Open the destination file for writing.
local file = assert(io.open(destination, "w"))

--Write the JSON data.
assert(file:write(jsonData))

--Close the file.
file:close()

print("Wrote file.json successfully!")
  • script.luafile.lua 放在同一个文件夹中,然后从那里运行script.lua
lua script.lua
  • 数据应该在file.json文件中编码成JSON,现在可以在Java下解析了。

【讨论】:

猜你喜欢
  • 2012-06-27
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多